Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpandableListView getChildrenCount not called after data changed, leading to IndexOutOfBoundsException

In my Activity, I change the underlying data HashMap for my ExpandableListView, and then call

((BaseAdapter) myExpandableListView.getAdapter()).notifyDataSetChanged();

However, the app crashes after calling this, raising an IndexOutOfBoundsException.

I double-checked that I access the same and right data everywhere in the adapter, but could not find any mistakes.

like image 492
FD_ Avatar asked Jun 24 '13 10:06

FD_


1 Answers

After a bit of debugging, I finally found out that, strangely enough, getChildrenCount is never called after I called notifyDataSetChanged. Thus, the Adapter is still expecting to get the same number of children as there were before the update. As I removed some children, that's a wrong assumption, and leads to the IndexOutOfBoundsException.

After a bit of playing around, I discovered that for ExpandableListViews, the Adapter has to be accessed via getExpandableListAdapter(), not via getAdapter(). After changing my code to

((BaseExpandableListAdapter) myExpandableListView.getExpandableListAdapter()).notifyDataSetChanged();

Everything works fine now.

I hope this helps any fellow developers. I spent a few hours finding and fixing this, and hardly found any information about it on the web and StackOverflow.

like image 154
FD_ Avatar answered Oct 30 '22 23:10

FD_