【问题标题】:For loop on ArrayList Model inside onPostExecute()onPostExecute() 内的 ArrayList 模型上的 For 循环
【发布时间】:2018-01-28 06:36:00
【问题描述】:

当我在onPostExecute() 期间过滤/检查我在ArrayList 中的模型项时遇到问题,我得到一个异常ConcurrentModificationException 试图访问/循环 通过"Items"

我有一个具有以下 inits 和 onCreateView() inits 的活动;

//model init
List<TrackingModel> Items;


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

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

在我通过URL 获取JSON 并在JSON 数据节点上执行循环之后,在onPostExecute() 内的项目循环期间发生此异常。

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

        }
     }
  }
}

是否需要在"Items" for-loop via 方法内添加数组?

outsideMethodAddToItems(id, another_string, more_string);

【问题讨论】:

标签: java android json arraylist


【解决方案1】:

这里发生错误,因为我们添加到 ArrayList 时无法执行 .add() for 循环 ....

ConcurrentModificationException 发生在您遍历列表并尝试在同一循环中修改(删除/添加)它时。这是不允许的。

相反,您可以创建另一个List 并继续向其中添加您的元素。

【讨论】:

  • 好的,我应该检查是否重复/存在并设置 Bool,然后在循环完成后检查 bool,如果 bool 为 false,则执行 add()...
  • @BENN1TH 你要去主List&lt;Boolean&gt;,然后什么时候不只是有另一个List&lt; TrackingModel &gt; 并继续添加你的元素呢?
  • 我认为只是在 jsonObj 的 for 循环中有一个临时布尔值,并在最后检查 Item for 循环...不需要创建另一个列表?
  • @BENN1TH 记住它是列表,您将经历多次迭代,并且您的布尔值将在任何迭代中被覆盖。那么如何使用简单变量而不是 List 呢?你想过吗?
  • 如果在 For 循环中找到项目,则临时布尔值设置为 true(默认设置为 false)
【解决方案2】:

我当前的解决方案是将临时变量(布尔值)设置为 false,如果项目匹配,则在循环内将临时布尔值设置为 true。然后我检查一下 temp Boolean 是否设置为 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++)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-10
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2012-10-09
    • 2015-11-12
    相关资源
    最近更新 更多