gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:32:35 25 4
gpt4 key购买 nike

在我的Activity 中,我更改了ExpandableListView 的基础数据HashMap,然后调用

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

但是,应用程序在调用此方法后崩溃,引发 IndexOutOfBoundsException

我仔细检查了我在适配器中的所有位置都访问了相同且正确的数据,但没有发现任何错误。

最佳答案

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

经过一番尝试,我发现对于 ExpandableListViews,适配器必须通过 getExpandableListAdapter() 访问,而不是通过 getAdapter()。将我的代码更改为

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

现在一切正常。

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

关于android - 数据更改后未调用 ExpandableListView getChildrenCount,导致 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17273589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com