Adding a "Child" or SubItem to ExpandableListView
I can't find instructions to add a child item to my expandable list anywhere. All I can find is 50 questions on how to change the background color.
Right now, I am using this code to create a normal list view:
public String listArray[] = { "Example1", "Example2", 开发者_C百科"Example3", "Example4", "Example5"};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
listV = (ListView) findViewById(R.id.exListView);
listV.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listArray));
}
But how would I go about adding sub or child items underneath example1, example2, etc..?
For starters, you need to use an ExpandableListView instead of a ListView. Then throw in a SimpleExpandableListAdapter and maintain a reference to your group (parent) and child data objects.
Then it's as easy as:
mChildData.get(groupIndex).get(childIndex).put(...);
mListAdapter.notifyDataSetChanged();
Basic example for expandablelistview is.. here....
Implement OnChildClickListener in your Activity and implemet this method..
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//Add new chid data in your list (ex ArrayLis or Array whatever you use)
adapter.notifyDataSetChanged() // BaseExpandableListAdapter's adapter...
return true;
}
Is it work work for you?
精彩评论