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()
orprevious()
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
Post a Comment