ExpandableListView 的 android notificationDataSetChanged 不起作用

发布于 2024-12-10 10:58:05 字数 384 浏览 4 评论 0原文

在我的应用程序中,我正在使用 ExpandableListView。我正在使用扩展BaseExpandableListAdapter的适配器。我想刷新 ExpandableListView

我的 ExpandableListView 由带有与数据库链接的删除按钮的项目组成。 如果我按下删除按钮,该项目将从数据库中永久删除。但是listview并没有同时刷新。如果我再次运行该活动,它会令人耳目一新,但不是同时进行。 我正在使用

mAdapter.notifyDataSetChanged();

,但它没有按我想要的方式工作。 为什么?

In my app I am working with ExpandableListView. I am using adapter which extends BaseExpandableListAdapter. I want to refresh the ExpandableListView.

My ExpandableListView consists of items with delete button which links with database.
If I'll press the delete button that item is permanently deleted from db. But the listview is not refreshing at the same time. If I again run that activity its refreshing but not at same time.
I am using

mAdapter.notifyDataSetChanged();

but its not working as I want.
Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

骄傲 2024-12-17 10:58:05

我刚刚在 BaseExpandableListAdapter 的以下重写方法上调用了 super,从那时起,notifyDataSetChanged() 就起作用了。

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        super.registerDataSetObserver(observer);    
    }

I just called super on the following overridden method of the BaseExpandableListAdapter and since then the notifyDataSetChanged() works.

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        super.registerDataSetObserver(observer);    
    }
乖乖公主 2024-12-17 10:58:05

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

you can try to additionally call invalidateViews() on your ExpandableListView when refreshing.

清风夜微凉 2024-12-17 10:58:05

你必须等到整个viewtree都被绘制出来,然后你可以调用notifyDataSetChanged()

“视图树观察者用于注册监听器,这些监听器可以通知视图树中的全局变化。这样的全局事件包括,但是不限于,整个树的布局,绘图过程的开始,触摸模式更改...... ViewTreeObserver 不应该由应用程序实例化,因为它是由视图层次结构提供的,请参阅 getViewTreeObserver() 了解更多信息。信息。”

http://developer.android.com/reference/ android/view/ViewTreeObserver.html

 final View v = convertView;
        ViewTreeObserver vto = convertView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            public void onGlobalLayout() {
                v.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                notifyDataSetChanged();     
}}); 

You have to wait until the whole viewtree has been drawn and then you can call notifyDataSetChanged()

"A view tree observer is used to register listeners that can be notified of global changes in the view tree. Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change.... A ViewTreeObserver should never be instantiated by applications as it is provided by the views hierarchy. Refer to getViewTreeObserver() for more information."

http://developer.android.com/reference/android/view/ViewTreeObserver.html

 final View v = convertView;
        ViewTreeObserver vto = convertView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            public void onGlobalLayout() {
                v.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                notifyDataSetChanged();     
}}); 
猫卆 2024-12-17 10:58:05

如果您使用 Big Nerd Ranch 的 Expandable 列表,请注意 RecyclerView.adapter 的传统 notifyDataSetChanged() 不起作用。

相反,Expandable RecyclerView 提供了一组通知方法,能够通知适配器 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)

有关更多详细信息,请访问 https://bignerdranch.github.io/expandable-recycler-view/

If you are using Expandable lists of Big Nerd Ranch, please note that traditional notifyDataSetChanged() of RecyclerView.adapter is not working.

Instead Expandable RecyclerView provides a set of notify methods with the ability to inform the adapter of changes to the list of 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)

For additional specifics visit https://bignerdranch.github.io/expandable-recycler-view/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文