PX Developer
PX Developer

Reputation: 8145

Android's ExpandableListView: Keep groups expanded/unexpanded when removing an item

I have an activity with an ExpandableListView. I fill the list using a custom adapter that extends BaseExpandableListAdapter (my adapter is called StopsExpandableAdapter). I implemented all the needed methods.

In my activity I can delete entries from that list using the context menu. When the user does it, I want to refresh the list so the item is removed. If I create a new adapter, the listview is reset to default and all the groups are collapsed. I want to keep the state of the listview, so expanded groups are still expanded after refreshing the list.

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

 mExpandableList.getAdapter();

I get a ExpandableListConnector, not my custom adapter, so I can't use my function. How can I refresh the list and keep its state?

Upvotes: 4

Views: 1777

Answers (2)

PX Developer
PX Developer

Reputation: 8145

To be able to access my adapter I save it as a global variable:

private StopsExpandableAdapter adapter;

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

To be able to mantain the state I'm using booleans to know if a group has been expanded or not. Everytime I add or remove an item I check which groups are expanded to be able to expand them again after the add/remove has been done.

private ArrayList<Boolean> expandGroups;

Upvotes: 1

Ajith M A
Ajith M A

Reputation: 5348

Use the following command.

StopsExpandableAdapter.notify();

or

StopsExpandableAdapter.notifydatasetchanged();

Upvotes: 0

Related Questions