0

I had problems updating a ListView (LV) and everytime I try to delete an item it promps me with the following error: The content of the adapter has changed but ListView did not receive a notification.

But I do call notify() on the LV here is my code:

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();
    }
}

And when I don't use the synchronized() it prompt the error: object not locked by thread before notify(); so I don't know what to do.

I call deleteNotify(Reminder reminder) from a OnClickEventListener() from one of the items inside the LV:

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);
}

Thanks.

Fransebas
  • 321
  • 4
  • 10
  • you should use notifyDataStateChange() method – Himeshgiri gosvami Jul 11 '17 at 16:38
  • Hi and welcome to Stack Overflow, please take a time to go through the [welcome tour](https://stackoverflow.com/tour) to know your way around here (and also to earn your first badge), read how to create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and also check [How to Ask Good Questions](https://stackoverflow.com/help/how-to-ask) so you increase your chances to get feedback and useful answers. – DarkCygnus Jul 11 '17 at 16:40

1 Answers1

2

You are applying notify on listView instead of adapter

where notify is a function comes from Object class basically for wait-notify locking mechanism which is not required here at all

solution : Apply notifyDataSetChanged on your adapter reference

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • Thanks! that worked, but now I wonder, Why is there a notify in first place? when to use one and when to use the other? – Fransebas Jul 11 '17 at 18:21
  • i mentioned something about that in the answer , you can take a look into small [example here](https://stackoverflow.com/questions/17398845/wait-notify-synchronization) and read about concurrency in java with thread , a whole new world is there to explore , good luck – Pavneet_Singh Jul 11 '17 at 18:25