从数组列表中删除元素后的java.util.ConcurrentModificationException android [英] java.util.ConcurrentModificationException android after remove elements from array list

查看:37
本文介绍了从数组列表中删除元素后的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)

推荐答案

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

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆