Android ExpandableListView: hold groups expanded / not expanded when deleting an item

I have working with ExpandableListView. I am populating the list using a custom adapter that extends BaseExpandableListAdapter (my adapter is called StopsExpandableAdapter). I have applied all the necessary methods.

In my activity, I can remove entries from this list using the context menu. When the user does this, I want to update the list so that the item is removed. If I create a new adapter the list is reset to default and all groups are collapsed. I want to preserve the state of the list, so the extended groups still expand after the list is refreshed.

I thought I could do this by adding a removeGroup method to my custom adapter (StopsExpandableAdapter) and use it in my activity after removing the group, but if I use:

 mExpandableList.getAdapter();

      

I am getting ExpandableListConnector, not my custom adapter, so I cannot use my function. How do I update the list and keep its state?

+3


source to share


2 answers


In order to have access to my adapter, I save it as a global variable:

private StopsExpandableAdapter adapter;

      

This way I can access my function to remove the child from my data list (inside the adapter) and report that the data has changed.



To be able to manipulate the state, I use a boolean to find out if the group has been expanded or not. Every time I add or remove an item, I check which groups will be expanded so that I can expand them again after the add / remove is complete.

private ArrayList<Boolean> expandGroups;

      

+1


source


Use the following command.

StopsExpandableAdapter.notify ();



or

StopsExpandableAdapter.notifydatasetchanged ();

0


source







All Articles