0

我正在尝试搜索包含某些数据的自定义 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);
        }
    }

}
4

1 回答 1

0

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

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();
        }
    }
}
于 2012-05-10T21:43:18.403 回答