Search code examples
androidexpandablelistviewexpandablelistadapter

Android: Multi-level ExpandableListView displaying children multiple times


I've created a 3-level (Root, Parent, Child) custom ExpandableListView and associated adapter thanks to some of the tutorials out there for this. I'm having trouble displaying the Parent level of the list, while Root and Child levels display correctly. What I'm finding is that the Parent level is duplicating its view N times, where N is the number of elements in the Parent group.

What I want: Three categories under Canon

What I'm getting: The three categories under Canon are duplicated 3 times

Root level:

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
                         View convertView, ViewGroup parent) {

/* this list is the header of the Parent level, and I know it has the correct number of elements */
List<String> lensTypeList = listDataChild.get(_listDataHeader.get(groupPosition));

/* the Parent level of the ExpandableListView */
LensSecondLevel lensSecondLevel = new LensSecondLevel(ManageLensesActivity.this);

/* all params other than lensTypeList work correctly in the later levels */
lensSecondLevel.setAdapter(new SecondLevelListViewAdapter(ManageLensesActivity.this, lensTypeList, lensMap, lensPositionIndicesMap));

getChildrenCount(int groupPosition) of Root level returns the correct number every time, despite how many children (Parent) views are created. I can't tell why getGroupView is getting called repeatedly for the Parent level.

Parent level:

@Override
public Object getGroup(int groupPosition)
{
    /* _listDataHeader is the same ArrayList<String> lensTypeList from above */
    return this._listDataHeader.get(groupPosition);
}

Aren't you supposed to pass an ArrayList<String> as the header for the 2nd level of the ExpandableListView? I can only achieve the desired result if I pass the 2nd level a single String as the header, but this breaks my logic for finding the Child level views, as groupPosition is always 0 since _listDataHeader.size() == 1

Thanks in advance for any help/advice. :)


Solution

  • Found the answer here: https://stackoverflow.com/a/35208411/5004391

    I don't really understand why but I had to hardcode getChildrenCount(int groupPosition) for the Root level to return 1, as opposed to the actual number of children. In my app's other ExpandableListViews I always return the number of children and it works as expected.