0

我得到了这个例外:

06-09 13:14:41.917: E/AndroidRuntime(630): java.util.ConcurrentModificationException
06-09 13:14:41.917: E/AndroidRuntime(630): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)  

我使用以下内容EndlessAdapter

  private class DemoAdapter extends EndlessAdapter {

    private RotateAnimation rotate=null;

    DemoAdapter(ArrayList<Outlets> outletList) {
        super(new OutletListAdapter(VerticalsListActivity.this, outletList));

        rotate=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
                                    0.5f, Animation.RELATIVE_TO_SELF,
                                    0.5f);
        rotate.setDuration(600);
        rotate.setRepeatMode(Animation.RESTART);
        rotate.setRepeatCount(Animation.INFINITE);
      }

    @Override
    protected View getPendingView(ViewGroup parent) {
      View row=getLayoutInflater().inflate(R.layout.row, null);

      View child=row.findViewById(android.R.id.text1);

      child.setVisibility(View.GONE);

      child=row.findViewById(R.id.throbber);
      child.setVisibility(View.VISIBLE);
      child.startAnimation(rotate);

      return(row);
    }
    @Override
    protected boolean cacheInBackground() throws Exception {
        // TODO Auto-generated method stub
        if(isMoreLoading){
            try {   
                if(Project.isFilterEnabled){
                    getDownloadedData = (FetchAndParseData) new FetchAndParseData().execute(Project.searchUrl+"/pageno/"+ ++i);
                }else{
                    getDownloadedData = (FetchAndParseData) new FetchAndParseData().execute(UrlConstants.OUTLETS_INFO_URL+Project.currentCityId+"/pageno/"+ ++i+"/verticalid/1");
                }
                inputStream = getDownloadedData.get();
                XmlUtilities.parseAndLoadData(inputStream , mOutletXmlHandler);
                outletList = Project.getOutletList();//Project.getOutletList();




            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }               
        return isMoreLoading;
    }

    @Override
    protected void appendCachedData() {
        // TODO Auto-generated method stub


        adptr = (OutletListAdapter) getWrappedAdapter();
        if(outletList.size()==20){
            isMoreLoading=true;
        }else{
            isMoreLoading=false;
        }           

        if(outletList!=null){
            for (Outlets addThis : outletList) {
                adptr.add(addThis);
            }
        }

    }

  }

现在,当一个列表包含少于 20 个元素并且我们滚动该列表时,它会引发上述异常。我在做什么错?

4

1 回答 1

4

阿伦,

当一个集合被多个线程修改时,会发生 ConcurrentModificationException。你cacheInBackground()让我相信你确实在利用额外的线程来获取你的数据。大多数情况下,当您在更改集合(例如排序)的同时添加或删除集合(例如缓存)时,会发生这种情况。

解决这个问题的最简单方法是在另一个线程中修改它之前复制集合(获取)。修改完成后,复制新集,去掉旧集。这允许您根据需要(在后台)更改当前设置,但在保持响应的同时根据需要进行 UI 更改。

希望这可以帮助,

模糊逻辑

于 2012-06-09T09:03:17.847 回答