[英]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"
在onPostExecute()
期间过滤/检查 ArrayList
中的模型项时onPostExecute()
,我遇到异常ConcurrentModificationException,试图通过"Items"
进行访问/循环
I have an activity that has the below inits and onCreateView()
inits; 我有一个具有以下inits和onCreateView()
init的活动;
//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. 在我通过URL
提取JSON
并在JSON
数据节点上执行了循环之后,在onPostExecute()
中的Items循环期间发生了此异常。
//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? 我需要通过方法添加到"Items"
for循环内的数组吗?
outsideMethodAddToItems(id, another_string, more_string);
error occurs here as we are adding to ArrayList cant do .add() when in for loop .... 错误发生在这里,因为我们在for循环中添加到ArrayList时无法执行.add()。
ConcurrentModificationException
occurs when your loop through the list and try modify (remove/add) it in the same loop. 当您在列表中循环并尝试在同一循环中修改(删除/添加)时,会发生ConcurrentModificationException
。 This is not allowed. 这是不允许的。
Rather, you could create another List
and keep adding your elements to it. 相反,您可以创建另一个List
并继续向其中添加元素。
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. 我当前的解决方案是将一个temp变量(boolean)设置为false,如果项匹配则在循环内将temp boolean设置为true。 I then do a check to see if the temp Boolean set to true, if not I can run add(); 然后,我进行检查以查看temp布尔值是否设置为true,否则可以运行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++)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.