android notifyDataSetChanged for ExpandableListView不工作

android notifyDataSetChanged for ExpandableListView不工作,android,expandablelistview,Android,Expandablelistview,在我的应用程序中,我使用的是ExpandableListView。我使用的适配器扩展了BaseExpandableListAdapter。我想刷新可扩展列表视图 MyExpandableListView包含带有与数据库链接的删除按钮的项目。 如果我按下删除按钮,该项将从数据库中永久删除。但是列表视图不会同时刷新。如果我再次运行该活动,它会让人耳目一新,但不会同时运行。 我正在使用 mAdapter.notifyDataSetChanged(); 但它没有像我想要的那样工作。 为什么?刷新时,您

在我的应用程序中,我使用的是
ExpandableListView
。我使用的适配器扩展了
BaseExpandableListAdapter
。我想刷新
可扩展列表视图

My
ExpandableListView
包含带有与数据库链接的删除按钮的项目。 如果我按下删除按钮,该项将从数据库中永久删除。但是
列表视图
不会同时刷新。如果我再次运行该活动,它会让人耳目一新,但不会同时运行。 我正在使用

mAdapter.notifyDataSetChanged();
但它没有像我想要的那样工作。
为什么?

刷新时,您可以尝试在ExpandableListView上另外调用invalidateViews()。

我刚才在BaseExpandableListAdapter的以下重写方法上调用了super,此后notifyDataSetChanged()就可以工作了


您必须等待整个viewtree绘制完成,然后才能调用notifyDataSetChanged()

“视图树观察者用于注册监听器,这些监听器可以被通知视图树中的全局更改。此类全局事件包括但不限于整棵树的布局、绘图过程的开始、触摸模式更改。。。。ViewTreeObserver不应由应用程序实例化,因为它是由视图层次结构提供的。有关详细信息,请参阅getViewTreeObserver()


如果您使用的是Big Nerd Ranch的可扩展列表,请注意,
RecyclerView.adapter
的传统
notifyDataSetChanged()

相反,可扩展的RecyclerView提供了一组notify方法,能够通知适配器ParentListItems列表的更改

// Parent Changes
notifyParentItemInserted(int parentPosition)
notifyParentItemRemoved(int parentPosition)
notifyParentItemChanged(int parentPosition)
notifyParentItemRangeInserted(int parentPositionStart, int itemCount)

// Child Changes
notifyChildItemInserted(int parentPosition, int childPosition)
notifyChildItemRemoved(int parentPosition, int childPosition)
notifyChildItemChanged(int parentPosition, int childPosition)

有关更多详细信息,请访问

您是否也重新查询了
光标
。?因为我只删除了该项目,因为该光标不是必需的。因此我不会在我将要写入的位置使用该光标?bcz我希望在删除按钮时单击“刷新”。每当您的可扩展列表视图的内容发生更改时。因此,在您删除某些条目后或者例如,但是有无效的eviews()不可用此方法必须在ExpandableListView对象上调用,而不是在适配器上。根据android java文档,ExpandableListView从超级类AbsListView继承了此方法:是的,我只使用该对象,但它不可用。不确定ExpandableListAdapter是如何作为接口的。我认为有点晚了,但实际上我需要我们将类:BaseExpandableListAdapter用作列表适配器..啊,好的,这很有意义,感谢您更新了答案。所以,您一定是重写了该方法而没有调用super。以前?我也不记得重写了该方法,但看起来Android Studio出于某种原因为我实现了它,而没有添加super。t汉克斯指出了这一点——我被这一点困扰了一段时间:)
 final View v = convertView;
        ViewTreeObserver vto = convertView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            public void onGlobalLayout() {
                v.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                notifyDataSetChanged();     
}}); 
// Parent Changes
notifyParentItemInserted(int parentPosition)
notifyParentItemRemoved(int parentPosition)
notifyParentItemChanged(int parentPosition)
notifyParentItemRangeInserted(int parentPositionStart, int itemCount)

// Child Changes
notifyChildItemInserted(int parentPosition, int childPosition)
notifyChildItemRemoved(int parentPosition, int childPosition)
notifyChildItemChanged(int parentPosition, int childPosition)