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);