从MapView的overlayList中删除Overlay的ConcurrentModificationException

时间:2012-05-10 21:23:49

标签: java android

我正在尝试搜索包含某些数据的自定义ItemizedOverlay ... 当我试图运行MapView的所有叠加层时,我在for循环上不断收到ConcurrentModificationException。

我希望有人知道这个问题的答案......

这是代码:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    Log.d("Activityresult", "voor ok");
    if(resultCode == RESULT_OK){
        GeoPoint point = new GeoPoint(touchedPoint.getLatitudeE6(), touchedPoint.getLongitudeE6());
        Car car = data.getExtras().getParcelable("car");
        PinpointItem pinpoint = new PinpointItem(point, car, DropHostCarActivity.this);
        CustomPinpointOverlay custom = new CustomPinpointOverlay(d, DropHostCarActivity.this);
        custom.insertPinpoint(pinpoint);
        List<Overlay> mapOverlays = mapView.getOverlays();
        if (mapOverlays.size() > 1) {
            removeSameCar(mapOverlays,car);
        }
        overlayList.add(custom);
        mapView.invalidate();
    }
}

private void removeSameCar(List<Overlay> mapOverlays, Car car) {
    for(Overlay overlay: mapOverlays){
        if(overlay instanceof CustomPinpointOverlay && 
                ((CustomPinpointOverlay) overlay).getItem(0).getCar().getNumberPlate().equals(car.getNumberPlate())){
            overlayList.remove(overlay);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

如果List实现支持,您只能在迭代时使用迭代迭代方法修改列表。您需要显式使用迭代器:

private void removeSameCar(List<Overlay> mapOverlays, Car car) {
    Iterator<Overlay> it = mapOverlays.iterator();
    while (it.hasNext()) {
        Overlay overlay = it.next();
        if(overlay instanceof CustomPinpointOverlay && 
                ((CustomPinpointOverlay) overlay).getItem(0).getCar().getNumberPlate().equals(car.getNumberPlate())){
            it.remove();
        }
    }
}