2012-05-10 67 views
0

我試圖搜索包含某些數據的自定義ItemizedOverlay ... 我一直在For循環時收到一個ConcurrentModificationException當我試圖通過所有覆蓋我的MapView。ConcurrentModificationException從MapView的overlayList刪除覆蓋

我希望有人知道這個問題的答案的問題...

下面的代碼:

@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); 
     } 
    } 

} 
+0

啊好吧,我現在已經制作了一個MapView的OverlayList的副本,並用它來放入for循環。這解決了最初的問題。直到現在,其他覆蓋層似乎也被刪除了......我將不得不搜索到這一點,非常感謝! :) – dumazy

+1

您可以在迭代時從列表中刪除項目,只要您使用迭代器進行迭代,並使用迭代器的remove()方法刪除項目。這避免了不必要的複製。 –

回答

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(); 
     } 
    } 
}