1

I am new in android. I want to remove item from Arraylist inside Arraylist. But when i removed particular item its given me an error 'ConcurrentModificationException'. So how can i handle it or if any other solution possible then please suggest me to solve this error. i am posting my code.

My code is as follow,

for (Item cart : CartItems) {
                if (cart.getIsSub_item() == false) {
                    for (Item list : sclList) {
                        CartItems.remove(list);
                    }
                } else {
                    for (Item i : sclList.get(sub_position).getSub_items()) {
                        if (CartItems.contains(i)) {
                            CartItems.remove(i);
                        }
                    }
                }
            }

Here is my logcat::

java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.util.ConcurrentModificationException
            at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
            at com.njoyful.userapp.Fragments.OrderFragment$RemoveSubItem.doInBackground(OrderFragment.java:800)
            at com.njoyful.userapp.Fragments.OrderFragment$RemoveSubItem.doInBackground(OrderFragment.java:774)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)

On first line, I am getting error. Thank you.

King of Masses
  • 18,405
  • 4
  • 60
  • 77
Das
  • 141
  • 13
  • post your logcat as well – King of Masses Aug 14 '15 at 04:53
  • Possible duplicate of [Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop](https://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re) – Raedwald Nov 16 '17 at 13:58

2 Answers2

0

ConcurrentModificationException

An ConcurrentModificationException is thrown when a Collection is modified and an existing iterator on the Collection is used to modify the Collection as well.

Seems from the comments that your ArrayList is accessed from the other method in one thread, by the UI, concurrently with you removing items from it in another thread.

So, why not just wrap both accessors in a

synchronized(array_list_name)
{
    // UI access code or item removal code
}

Note that this might make your UI laggy if removing items takes a long time. If this is the case, consider making a list of all item indexes to be removed, and remove them in a tight synchronized loop after iterating over the whole list.

In another way , Try to using java.util.concurrent.CopyOnWriteArrayList instead of ArrayList if there are few modifications.

King of Masses
  • 18,405
  • 4
  • 60
  • 77
  • look into synchronized https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html – King of Masses Aug 14 '15 at 05:27
  • and i cant use java.util.concurrent.CopyOnWriteArrayList. because its very large modification in my code... – Das Aug 14 '15 at 05:48
0

Instead Use Iterator to remove item from ArrayList.

                Iterator<Items> iter = CartItems.iterator();

                while(iter.hasNext()){

                    if("your condition")
                    {
                        iter.remove();
                    }
                    else{
                         "Your code"}
                }
Ankit Gupta
  • 674
  • 1
  • 6
  • 17