you need to create adapter which extends BaseExpandableListAdapter
it will manage child view and parent view . see this for details.
How do I create an expandable list view in android from a List of Objects?
-
20-06-2023 - |
Frage
I have a list of objects in android. I want to iterate through the list and create an expandable list view, with one entry per object. I already have the ExpandableListView
created in the xml file. So i Know I start with:
ExpandableListView results = ((ExpandableListView)rootView.findViewById(R.id.results));
I want to go through the list of Objects and create a Parent
for each object, and each Child
will be an instance variable of the main Object.
for instance:
Parent = Object.Title
Child1 = Object.Taste
Child2 = Object.smell
Parent1 = Object1.Title
Child1 = Object1.Taste
Child2 = Object1.smell
Lösung
Andere Tipps
You have to basically extend your adapter with BaseExpandableListAdapter
and override methods of it.
here is a good example
http://theopentutorials.com/tutorials/android/listview/android-expandable-list-view-example/
Extend your custom Adapter with BaseExpandableListAdapter and ovverride getChildView(...) and getGroupView(...) like this:
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
tempChild = (ArrayList<String>) Childtem.get(groupPosition);
TextView text = null;
if (convertView == null) {
convertView = minflater.inflate(R.layout.childrow, null);
}
text = (TextView) convertView.findViewById(R.id.textView1);
text.setText(tempChild.get(childPosition));
convertView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(activity, tempChild.get(childPosition), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = minflater.inflate(R.layout.grouprow, null);
}
((CheckedTextView) convertView).setText(groupItem.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
See complete sample source code here