简体   繁体   中英

For loop on ArrayList Model inside onPostExecute()

I have an issue when filtering/checking my model items in ArrayList during onPostExecute() which I get an exception ConcurrentModificationException trying to access/loop through "Items"

I have an activity that has the below inits and onCreateView() inits;

//model init
List<TrackingModel> Items;


//onCreateView() {}
Items = new ArrayList<>();

//and prompt async task
new RetrieveFeedTask().execute();

This exception occurs during the Items loop inside onPostExecute() after I have fetched JSON via URL and done a loop on the JSON data nodes.

//For Loop on JSON Response in onPostExecute()
JSONArray data = obj.getJSONArray("response");
for (int i = 0; i < data.length(); i++) {

   String id = data.getJSONObject(i).optString("id");

   //in here I add to Items, first checking if Items.isEmpty()
   if(Items.isEmpty()){

     //add to Model/Items.ArrayList
     //works fine

     TrackingModel reg = new TrackingModel();                                                            
     reg.setId(id);
     Items.add(reg);

   }else{

     //check getJSONObject() item already in Items.ArrayList to avoid duplications

     for (TrackingModel Item : Items) {

        if(Item.id().toString().contains(id)){

             //already in ArrayList, skip adding

        }else{

            //error occurs here as we are adding to ArrayList
            //cant do .add() when in for loop ....

            //Do I add to the array outside the For Loop via method?
            //outsideMethodAddToItems(id, another_string, more_string);

            TrackingModel reg = new TrackingModel();                                                            
            reg.setId(id);
            Items.add(reg);

        }
     }
  }
}

Do I need to add to the array inside the "Items" for-loop via method?

outsideMethodAddToItems(id, another_string, more_string);

error occurs here as we are adding to ArrayList cant do .add() when in for loop ....

ConcurrentModificationException occurs when your loop through the list and try modify (remove/add) it in the same loop. This is not allowed.

Rather, you could create another List and keep adding your elements to it.

I current solution is to have a temp variable(boolean) which is set to false and inside the loop if a item matches set the temp boolean to true. I then do a check to see if the temp Boolean set to true, if not I can run add();

 //while inside 
 JSONArray data = obj.getJSONArray("response");
 for (int i = 0; i < data.length(); i++) {

    //temp boolean
    Boolean isFound = false;

    for (TrackingModel Item : Items) {

      if(Item.id().toString().contains(id)){

         //already in ArrayList, skip adding
         //set temp boolean as true as we found a match
         isFound = true;

      }


   }

   //now we check temp boolean isFound is false, so we can run add();
   if(!isFound ){

        TrackingModel reg = new TrackingModel();                                                            
        reg.setId(id);
        Items.add(reg);

   }

}
//end of for (int i = 0; i < data.length(); i++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM