【问题标题】:ExpandableListView getChildrenCount not called after data changed, leading to IndexOutOfBoundsException数据更改后没有调用 ExpandableListView getChildrenCount,导致 IndexOutOfBoundsException
【发布时间】:2013-06-20 20:21:52
【问题描述】:

在我的Activity中,我把底层数据HashMap换成我的ExpandableListView,然后调用

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

但是,应用程序在调用此函数后崩溃,并引发 IndexOutOfBoundsException。

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

【问题讨论】:

    标签: android expandablelistview indexoutofboundsexception notifydatasetchanged


    【解决方案1】:
    listAdapter.notifyDataSetChanged()
                   
    val groupPosition = 0
    expandableListView.expandGroup(groupPosition, true)
    

    我通过在调用notifyDataSetChanged() 之后添加方法expandableListView.expandGroup() 解决了我的问题。

    【讨论】:

      【解决方案2】:

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

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

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

      现在一切正常。

      我希望这可以帮助任何其他开发人员。我花了几个小时来查找和修复这个问题,但在网络和 StackOverflow 上几乎找不到任何关于它的信息。

      【讨论】:

        猜你喜欢
        • 2015-11-28
        • 2020-10-10
        • 2018-12-23
        • 1970-01-01
        • 2019-10-23
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        相关资源
        最近更新 更多