从数组列表中删除元素后java.util.ConcurrentModificationException android

brp*_*paz 17 android exception arraylist java.util.concurrent

我在我的Android应用程序中有以下代码:

/**
 * callback executed after fetching the data.
 */
public void OnPointsFetch(ArrayList<Shop> result) {

    toggleLoader(false);

    this.shops = result;

    if(activeFilter == Constants.POINTS_FILTER_AVAILABLE){
        for(Shop s : result){
            if(s.getClientPoints().getPointsAvailable() == 0){
                this.shops.remove(s);
            }
        }
    }
    else{
        for(Shop s : result){
            if(s.getClientPoints().getPointsSpent() == 0){
                this.shops.remove(s);
            }   
        }
    }


    ptsListAdapter.setCollection(this.shops);
    ptsListAdapter.setFilter(this.activeFilter);

}
Run Code Online (Sandbox Code Playgroud)

在异步任务的结果上调用此方法.我需要在传递给列表适配器之前删除集合的一些元素.

    11-23 17:39:59.760: E/AndroidRuntime(19777): java.util.ConcurrentModificationException
11-23 17:39:59.760: E/AndroidRuntime(19777):    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
Run Code Online (Sandbox Code Playgroud)

ass*_*ias 46

迭代时,您无法从列表中删除项目.您需要使用迭代器及其remove方法:

for(Iterator<Shop> it = result.iterator(); it.hasNext();) {
    Shop s = it.next();
    if(s.getClientPoints().getPointsSpent() == 0) {
        it.remove();
    }   
}
Run Code Online (Sandbox Code Playgroud)