How add subitems with array - android

I want to make a simple application that contains a listview and underneath a subitem to say something more than that button.
In this case big say "Hi" and "Sub Hi" in subitem.
I have read in places that you have to create a separate class but the codes that are difficult to get.
Is there a simple example where you can just walk array (as in my code) for each array, and then combine them to tell you the item?
i want something like this: ( do it with paint)
final String[] test={"Hi"};
String[] subtest={"Sub HI"};
ListView list = (ListView)findViewById(R.id.LV_lista);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, prueba);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subprueba);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
String NombreHabilidad = prueba[position];
Toast.makeText(getApplicationContext(), "u click "+ NombreHabilidad, Toast.LENGTH_SHORT).show();
}
});

You need to use a ExpandableListView.
Try this tutorial.

Related

Updating list elements on Spinner list item selection - Android

A little context: I have a stable tutorials app on the market. Recently the users have been requesting some external sources where they can learn more. This question is regarding the solution I came up with.
I have an activity with a listview and a spinner on top.
The listview can be populated with up to 25 elements at once (should I make it lesser?) .... I want the spinner to have options like (1-25, 26-50, 51-75).
The default can be set to 1-25. Depending on what the user selects, I need to re-populate the listview accordingly. I reckon a two dimensional array would be the neatest. But I don't want to hinder the performance too.
Anyways, here's what I've done yet. I need to know how exactly should I update the listview elements in the Spinners OnClick.
Spinner spinner;
String path[] = {"Laptops","DesktopPC","Tablets","Add-Ons","Gaming"};
String Laptops[] = {"Dell","Hp","Apple"};
ListView lstView;
lstView = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (Category.this, android.R.layout.simple_spinner_item, path );
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
ArrayAdapter<String> lstAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Laptops);
lstView.setAdapter(lstAdapter);
//lstView.refreshDrawableState();
}
});
Assume that I have to switch between the two arrays depending on the spinner selection.
Thank you for reading! If you require any more information, kindly ask :-)

Compare Strings in List View in android

I am developing an Android app in which there are two list views. When user clicks on items of first list view that must be added to second list view. This is I have implemented correctly. But my problem is strings must not be repeated in second list view. Means if String A is there in list view 1 and I click on it, it moves to list view 2 and if again I click on String A, it should prompt message(Message will be handled by me). Here is my code. What am I missing?
ArrayList<String> arr2;
ArrayAdapter<String> adapter2;
arr2 = new ArrayList<String>();
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String selected = lv1.getItemAtPosition(arg2).toString();
arr2.add(selected);
adapter2 = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, arr2);
lv2.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
}
});
probably this will work.
String selected = lv1.getItemAtPosition(arg2).toString();
if (!arr2.contains(selected)) {
//add item in second ListView
arr2.add(selected);
} else {
show popup
}
Finally, got it....
if (!arr2.contains(selected)) {
arr2.add(selected);
adapter2 = new ArrayAdapter < String > (getBaseContext(), android.R.layout.simple_list_item_1, arr2);
lv2.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
} else {
Toast.makeText(getBaseContext(), "Already Contains", 1000).show();
}
I would suggest you save an ArrayList (your arr2) and check if arr2 already contains the string selected.
if(arr2.contains(selected)){
//continue with something else
}else{
//add to list and refresh Fragment
}
Use Contains method and check whether particular value exists in arr2 if it returns false then only perform arr.add(selected)
Change this
arr2.add(selected);
To
if(!arr2.contains(selected)){
arr2.add(selected);
}

How to add a click listener on auto generated Android ListViews

I added to my class MyActivity the following :
private void updateMyList(){
listing=new ArrayList<listing>();
for(int i =0;i<10;i++)
{
Users user=new Users();
user.setListingName("Name" + i);
user.setListingPhone("i" + i);
listing.add(user);
}
MyListAdapter lfa = new MyListAdapter(this, listing);
((ListView)findViewById(R.id.listFeed)).setAdapter(lfa);
}
This code generates 10 list views so I'd like to add a click listener, so when I click on one of the 10 lists, I get a message or sthing.
Thank you for your help.
I don't see why you couldn't just add an onItemClickListener in your loop to your ListView. In short, use your Adapter to create the list then just attach the listener:
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, connections.toArray(new String[connections.size()])));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View item, int position, long id) {
String item = (String) lv.getItemAtPosition(position);
}
});
This is if you want to know which item in each list was clicked, there is an setOnClickListener method as well in case you just want to know whether a ListView has been clicked or not.

Sorting ListBiew by Spinner in android?

I have one ListView and one Spinner. The Spinner is situated at the right top of the layout. My ListView contains some names (eg. country names) and the spinner has some alphabets (a-z). Suppose if I choose the letter "f" from the Spinner, my ListView must show the country names that starts only with the letter "f" . I want to sort my ListView by values from the Spinner?
You need to implement a Filter for your ListView. Everytime the OnItemSelectedListener of your Spinner gets called you need to filter the items. If you're not sure how to implement a filter in your ListView's adapter have a look at this: Filtering ListView with custom (object) adapter
I guess there won't be any way around implementing an own ListAdapter (it's easy).
Try to use the getFilter() method of the ListView adapter:
String[] filterL = { "a", "b", "c" }; //etc
//...
Spinner spin = (Spinner) findViewById(R.id.spinner1);
final ArrayAdapter<String> aspin = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filterL); //the adapter for the Spinner
aspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aspin);
final ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, countries); //the adapter for the list
setListAdapter(aa); //set the adapter for the list(if you extend LisActivity) or call setAdapter on the ListView element
//add the listener:
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
boolean status = false;
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
if (!status) {
status = true;
return;
}
aa.getFilter().filter(filterL[position]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});

android listview attribute

Is listview has any attribute (e.g. productId, like hidden field of html form) can be set for later use? The following is to build a listview by using an array
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, catlist);
ListView listView = (ListView)findViewById(R.id.ListView01);
listView.setAdapter(adapter);
The listview will display the text inside of the catlist, i want to pass the productid which corresponding the category text to another activity when users click on every item.
listview.setOnItemClickListener(new OnItemClickListner()) {
}
You can do something like this:
You will have two arrays catlist and catIds, catlist will have name to show in ListView and catIds will have ids at same indices, now when the user click any item, you will get its position and grab the id from catIds at that position like this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> pr, View v, int position, long id) {
String id = catIds.get(position);
//use this id for any purpose.
}
});

Categories

Resources