I have created 2 listviews in 1 Activity.
Eg:Vegetables
Fruits
Meat
So when you click on Fruits I want all the 5 Fruits available to show in the 2nd listView. Can you please tell me how to do this? My code is given below
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listv);
ListView menu= (ListView) findViewById(R.id.listView1);
String items[]= {"Fruits","Vegetables","Jooses","Meat","Toys","Cookeys"};
ListView menu2= (ListView) findViewById(R.id.listView2);
String subitems[]= {"xxx","xxx","xxx","xxx","xxx","xxx"};
menu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems));
}
Set the adapter for the second listview in the SetOnItemClick adapter of first listview:
menu.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, check which item is it and then set adapter and display values in 2nd listview
}
});
Refer:
http://matrix-examplecode.blogspot.in/2011/11/listview-example.html
http://www.mkyong.com/android/android-listview-example/
http://developer.android.com/reference/android/widget/AdapterView.html
## -----lets try this simple code. this will suites your need-----##
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listv);
ListView menu= (ListView) findViewById(R.id.listView1);
ListView menu2= (ListView) findViewById(R.id.listView2);
String items[]= {"Fruits","Vegetables","Jooses","Meat","Toys","Cookeys"};
String subitems1[]= {"xxx1","xxx","xxx","xxx","xxx","xxx"};
String subitems2[]= {"xxx2","xxx","xxx","xxx","xxx","xxx"};
String subitems3[]= {"xxx3","xxx","xxx","xxx","xxx","xxx"};
String subitems4[]= {"xxx4","xxx","xxx","xxx","xxx","xxx"};
String subitems5[]= {"xxx5","xxx","xxx","xxx","xxx","xxx"};
menu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
// on item click listener
menu.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// get the selected values from listview 1
String val = items[arg2];
if(val.equalsIgnoreCase("Fruits"))
{
// set the second listview 2
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems1));
}
else if(val.equalsIgnoreCase("Vegetables"))
{
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems2));
}
else if(val.equalsIgnoreCase("Jooses"))
{
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems3));
}
else if(val.equalsIgnoreCase("Meat"))
{
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems4));
}
else if(val.equalsIgnoreCase("Toys"))
{
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems5));
}
}
});
}
Register an onItemClickListener callback on menu (Listview).
menu.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick (AdapterView<?> parent, View view, int position,
long id)
{
/* TODO: Update menu2 adapter items */
/* Position - determines the item that is being clicked on your menu listview,
by that. you can render your second adapter based on the item selected */
/* Call adapter notifyDatasetChanged method to inform adapter that its data set
has changed. */
menu2Adapter.notifyDatasetChanged();
}
});
Or other design may be. Put your 2nd listview on a second activity. every time you
select an item. Push the new activity that points to the 2nd list view..
when you click an item in the left listview, the OnItemCLickListener will be triggered. In the callback, update the underlying data maintained by the second listview and call notifyDataSetChanged.
Check the following answer...I checked this code by myself. May be what you want to do.
ListView menu = (ListView) findViewById(R.id.listView1);
String items[] = { "Fruits", "Vegetables", "Jooses", "Meat", "Toys", "Cookeys" };
final ListView menu2 = (ListView) findViewById(R.id.listView2);
final String subitems[] = { "xxx", "xxx", "xxx", "xxx", "xxx", "xxx" };
menu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//Here YourActivity.this means the Context
menu2.setAdapter(new ArrayAdapter<String>(YourActivity.this,
android.R.layout.simple_list_item_1, subitems));
}
});
Override your first listview(menu in your example) onItemClickListener
and prepare subitems (list to display in menu2) according to your need
or whatever you wants to display and call notifyDataSetChanged() on
menu2 adapter ( menu2.getAdapter().notifyDataSetChanged(); ).
How can i change image from adapter.
I have created a ListView and in this listview i have added dynamic item which has name and picture. I want to change the pic from that item on some logic.
adapter is created by below code
SimpleAdapter adapter = new SimpleAdapter(this, menuItems , R.layout.list_item, new String[] { NAME, EMPID }, new int[] { R.id.name, R.id.empid });
setListAdapter(adapter);
ListView lv = getListView();
below Code works fine if under OnItemClickListener. but i want image in at the time of rendering first view.
lv.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(XMLUtil.getNodeValue(group, "Gender").equalsIgnoreCase("male")){
view.findViewById(R.id.imageView).setBackgroundResource(R.drawable.male);
}else{
view.findViewById(R.id.imageView).setBackgroundResource(R.drawable.female);
}
}
Set new image in menuItems and call adapter.notifyDataSetChanged() for updating your ListView
How can I insert an OnClickListener for list view? I want to show an alert dialog with two buttons, "yes" and "no", when I click an item.
private void loadListViewData() {
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<String> lables = db.getAllLabels();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line,
lables);
listview.setAdapter(dataAdapter);
}
If you want to set Click Listener on item, then use setOnItemClickListener, on listview, using this method you will have access to clicked item position too, simply do as below:
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView adapterView, View convertView, int position, long id)
{
//Do some operation
}
});
I am trying to get the selected value out of a single choice list view and it won't let me use the setOnItemClickListener any ideas?
final ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice, values);
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList = (lv.getItemAtPosition(myItemInt));
}
}
modify like this: String selectedFromList = (String) (lv.getItemAtPosition(myItemInt));
you don't need to bind the "onItemClickListener", just use "getCheckedItemPosition" and you can get the checked position. then get the selected item from your data source.
I wanted to display my data into android spinner from database.
I used two spinner..
2nd spinner should reflect once 1st spinner item is selected, every thing is working fine..data is loaded into the second spinner, but not displaying into 2nd spinner when 2nd spinner item is selected.
Spinner1 = (Spinner)findViewById(R.id.createProfileCitySpinnerId);
Spinner2 = (Spinner)findViewById(R.id.createProfileStateSpinnerId);
//for 1st spinner.....(working)
final List<String> list1 = new ArrayList<String>();
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,list1);
Spinner1.setAdapter(adapter1);
//for second spinner...
final List<String> list2 = new ArrayList<String>();
Spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(//some condition using id)
{
list2.add(stateCursor.getString(1));
}
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
//everything is working data is loading, but not display once item is selected on 2nd spinner
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list2);
stateSpinner.setAdapter(adapter2);
You need to add the element directly to the adapter (not to list2), the ArrayAdapter keeps its own internal data. Try this code on your onItemSelected:
adapter2.add(stateCursor.getString(1));
adapter2.notifyDataSetChanged();
Call adapter2.notifyDataSetChanged();
such as
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(//some condition using id)
{
list2.add(stateCursor.getString(1));
adapter2.notifyDataSetChanged();
}
}
see http://developer.android.com/reference/android/widget/ArrayAdapter.html for more information.
public void notifyDataSetChanged () Since: API Level 1
Notifies the attached observers that the underlying data has been
changed and any View reflecting the data set should refresh itself.