i am new to android and i want to know how to implement Multiple Listview in android so i am create an app that contain 4 String Arrays
<string-array name="menu1">
<item>Break Fast</item>
<item>Lunch</item>
<item>Dinner</item>
</string-array>
<string-array name="breakfast">
<item>idly</item>
<item>dosa</item>
<item>pongal</item>
<item>puri</item>
<item>chapathi</item>
</string-array>
<string-array name="lunch">
<item>Meals</item>
<item>Curd Rice</item>
<item>Biriyani</item>
<item>Tomato Rice</item>
<item>Chapathi</item>
</string-array>
<string-array name="dinner">
<item>idly</item>
<item>dosa</item>
<item>parotta</item>
<item>puri</item>
<item>chapathi</item>
</string-array>
when i click the Menu 1 item Breakfast another listview want to be called in that listview it want to display the item related in Breakfast String Array simulary the other items are want to be called how can we done this
MainActivity
public class MainActivity extends ActionBarActivity {
ListView listView;
String[] menus;
String[] breakfast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listView);
menus=getResources().getStringArray(R.array.menu1);
breakfast=getResources().getStringArray(R.array.breakfast);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,R.layout.first_menu,R.id.textView,menus);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent= new Intent(MainActivity.this,MenuTwo.class);
}
});
}
In onClick listener make a switch case, update menu data and call notifydatasetchanged :
switch(item)
{
case 0: menu = ....; break;
case 1: menu = ....; break;
...
}
adapter.notifydatasetChanged();
You will also need to update on click listener. Better way, use a new Listview to displaye relative items on click
Take a look at this tutorials, they are implementing expandable listviews. I think that's what you need.
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
http://techlovejump.com/android-multilevel-listview-tutorial/
Also, you can use AndroidTreeView project from github
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With