繁体   English   中英

如何从另一个视图通知一个Android ListView?

[英]How to notify an android ListView from another view?

我在更新ListView(LV)时遇到问题,并且每次尝试删除项目时,都会出现以下错误提示我: The content of the adapter has changed but ListView did not receive a notification

但是我确实在LV上调用notify() ,这是我的代码:

public void deleteNotif(Reminder reminder)
{
    NotificationDBHelper db = new NotificationDBHelper(getBaseContext());
    this.mNotificationsHours.remove(reminder); // here I remove the element from the db
    db.delete(reminder); // here I remove the element from the list

    synchronized (this.mNotificationListLV)
    {
        Log.d("sync called","hi stackoverflow");
        this.mNotificationListLV.notify();
    }
}

当我不使用synchronized() ,会提示错误: object not locked by thread before notify() 所以我不知道该怎么办。

我从LV内部的项目之一从OnClickEventListener()调用deleteNotify(Reminder reminder)

public NotificationButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    //getAttr(context, attrs);
    this.mImg.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v)
        {
            NotificationConfig nc = NotificationConfig.getInstance();
            nc.deleteNotif(mReminder);
        }
    });
    super.setImg(R.drawable.ic_delete_forever_black_24dp,0xc40003);
}

谢谢。

您正在对listView而不是适配器应用notify

其中notify是一个函数,它基本上来自Object类,用于wait-notify锁定机制,这里根本不需要

解决方案:在您的适配器参考上应用notifyDataSetChanged

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM