ConcurrentModificationException

ConcurrentModificationException

本文介绍了从数组列表中删除元素后,java.util.ConcurrentModificationException android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的android应用程序中包含以下代码:

I have the folloing code in my android app:

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

}

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

This method is called on the result of an async task. I need to remove some elements of the collection before passing to the list adapter.

    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)


推荐答案

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

You can't remove items from a list while iterating over it. You need to use an iterator and its remove method:

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

这篇关于从数组列表中删除元素后,java.util.ConcurrentModificationException android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:20