数据更改后未调用ExpandableListView getChildrenCount,导致IndexOutOfBoundsException

FD_*_*FD_ 5 android expandablelistview indexoutofboundsexception notifydatasetchanged

在我Activity,我改变底层的数据HashMap为我ExpandableListView,然后调用

((BaseAdapter) myExpandableListView.getAdapter()).notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)

但是,在调用此应用程序后,应用程序崩溃了IndexOutOfBoundsException.

我仔细检查了我在适配器中到处访问相同和正确的数据,但找不到任何错误.

FD_*_*FD_ 6

经过一些调试后,我终于发现,奇怪的是,getChildrenCount在我打电话之后,它从未被调用过notifyDataSetChanged.因此,适配器仍然期望获得与更新之前相同数量的子节点.当我删除一些孩子时,这是一个错误的假设,并导致了IndexOutOfBoundsException.

经过一段时间的游戏后,我发现ExpandableListViews必须getExpandableListAdapter()通过getAdapter()访问适配器.将我的代码更改为

((BaseExpandableListAdapter) myExpandableListView.getExpandableListAdapter()).notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)

现在一切都很好.

我希望这可以帮助任何开发人员.我花了几个小时找到并解决这个问题,几乎没有在网络和StackOverflow上找到任何关于它的信息.