Search code examples
androidsavestateexpandablelistviewrestore

How to save and restore the state of an ExpandableListView in Android?


Is it possible to save and restore the state (which items are collapsed and which not) of an ExpandableListView in Android?

If it is possible, how can I do that?

Can I access the ExpandableListView in onPause() / onResume() and how?


Solution

  • I iterate through the groups and save the state of them all:

    int numberOfGroups = MyExpandableListViewAdapter.getGroupCount();
    boolean[] groupExpandedArray = new boolean[numberOfGroups];
      for (int i=0;i<numberOfGroups;i++)
        groupExpandedArray[i] = MyExpandableListView.isGroupExpanded(i);
    

    Then for restore the state:

    for (int i=0;i<groupExpandedArray.length;i++)
      if (groupExpandedArray[i] == true)
        MyExpandableListView.expandGroup(i);
    

    I don't understand what you mean by how to access the ListView in onPause() / onResume(), it should be accessible from there if you store the ListView object as a member of the activity class.