我的 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);
}
在异步任务的结果上调用此方法。在传递给列表适配器之前,我需要删除集合的一些元素。
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 方法:
for(Iterator<Shop> it = result.iterator(); it.hasNext();) {
Shop s = it.next();
if(s.getClientPoints().getPointsSpent() == 0) {
it.remove();
}
}
关于android - 从数组列表中删除元素后的java.util.ConcurrentModificationException android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13533715/