java - Remvoing object from a list while iterating it doesn't work (with iterator) -


this method:

public void deleteevent(view view){         intent intent = new intent(this, mainactivity.class);          (user myuser:localdatabase.geteventbyid(eventid).getinviteduserstotal()){             (event myevent:myuser.getattendinglist())             {                 listiterator<event> itr_attending =myuser.getattendinglist().listiterator();                 if (myevent.getid()==eventid){                     itr_attending.remove();                 }              }         }          toast.maketext(getapplicationcontext(), "the event deleted",toast.length_long).show();         startactivity(intent); } 

this method in order delete event database. in order need remove "attending list" of each invited user. (if user says going event, event moving list of him).

each event has list of invited users (inviteduserstotal), , each user, method should go on user's attending list , serach wanted event(myevent.getid()==eventid), , delete it.

in end, event not deleted attending list.

what doing wrong here?

read javadoc of listiterator.remove():

removes list last element returned next() or previous()

you're never calling next() or previous() on list iterator, there's nothing remove.


it's not clear intending remove list - fact have 2 iterators concurrently on same list suggests - if did remove - you'd concurrentmodificationexception anyway.

i think need replace inner enhanced loop explicit iteration: can't remove list you're iterating enhanced loop:

for (user myuser: ...){   iterator<event> = myuser.getattendinglist().iterator();   while (it.hasnext()) {     event myevent = it.next();     if (myevent.getid()==eventid){       it.remove();     }   } } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -