Search code examples
javaandroidlistconcurrentmodification

ConcurrentModificationException at for each android


I am passing Arraylist of ParseObject, and then i am putting one one foreach loop to extract the items with a condition when user object is not equals to null. There are two problems which i am facing. 1. If i am doing the following lines of code by passing different data to another list and then pass that list in my adapter, i am getting random data with numbers for example: If on item # 1 the name is "MAC" then it is showing in item 3.

ArrayList<ParseObject> checkRequestedNetArrayList = new ArrayList<ParseObject>();
requestedNetArrayList = (ArrayList<ParseObject>) objects;
MyResponsibilitesActivity.requestedNetArrayList = requestedNetArrayList;
adapterRequest = new GenericAdapter<ParseObject>(
    getApplicationContext(),
    requestedNetArrayList,
    R.layout.requested_trust_net_list_item,
    requestedDataBinder);

requestListView.setAdapter(adapterRequest);

requestedNetArrayList = (ArrayList<ParseObject>) objects;

for(ParseObject object: objects){
    System.out.println(object);
    object.getParseObject("user");
    if(object.has("user")){

        checkRequestedNetArrayList.add(object);

    }else{
        checkRequestedNetArrayList.remove(object);
    }

}

adapterRequest = new GenericAdapter<ParseObject>(
    getApplicationContext(),
    checkRequestedNetArrayList,
    R.layout.requested_trust_net_list_item,
requestedDataBinder);

requestListView.setAdapter(adapterRequest);
  1. If i am doing the following line of code to just direct giving the items in the same list, i am getting the java.util.ConcurrentModificationException

    for(ParseObject object: objects){
    
        if(object.has("user")){
    
            requestedNetArrayList.add(object);
    
        }
    }
    else{
        requestedNetArrayList.remove(object);
    }
    
    adapterRequest = new GenericAdapter<ParseObject>(
        getApplicationContext(),
        requestedNetArrayList,
        R.layout.requested_trust_net_list_item,
        requestedDataBinder);
    
    requestListView.setAdapter(adapterRequest);
    

    }

Please help me out here.


Solution

  • You can not remove an element from list while accessing it. You have to use Iterator.

    Where ever you are removing the object, use it.remove();

    Iterator<ParseObject> it = objects.iterator();
    while(it.hasNext()){
        Object object = it.next();
        //your stuff
        it.remove();
    }
    

    I think you might want to check this article about deep copy also.

    UPDATE

    Since you want to add elements to the list it is not directly possible with iterator. Now you are facing problem because you are directly assigning objects to requestedNetArrayList instead of that do it in the following way :

        ArrayList<ParseObject> requestedNetArrayList = new ArrayList<>(objects);
    

    Then iterate over objects as you are doing now, and remove from or add to requestedNetArrayList (which you are pretty much already doing).