I have a checkboxlist where the user can select some routes then a reponse is being getting from the server. I have method gotoLocation to upadte the location of the markers as well to add a new marker in the map when a new one is being inserted into the table on the serverside with the same route.
I tried it before with HashMap<Integer, Marker> as <id, marker> but I had problem to add a new marker of the new request to the map so I tried it with ArrayList<Integer, String> and it works but I am getting now the error below when I try to remove a marker from the map.
How can I fix it?
Error:
07-16 00:19:05.663: E/AndroidRuntime(29919): FATAL EXCEPTION: main
07-16 00:19:05.663: E/AndroidRuntime(29919): Process: com.bustracker, PID: 29919
07-16 00:19:05.663: E/AndroidRuntime(29919): java.util.ConcurrentModificationException
07-16 00:19:05.663: E/AndroidRuntime(29919): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
07-16 00:19:05.663: E/AndroidRuntime(29919): at com.bustracker.Map.gotoLocation(Map.java:132)
07-16 00:19:05.663: E/AndroidRuntime(29919): at com.bustracker.Map.onNewIntent(Map.java:314)
07-16 00:19:05.663: E/AndroidRuntime(29919): at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1224)
07-16 00:19:05.663: E/AndroidRuntime(29919): at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2833)
07-16 00:19:05.663: E/AndroidRuntime(29919): at android.app.ActivityThread.performNewIntents(ActivityThread.java:2846)
07-16 00:19:05.663: E/AndroidRuntime(29919): at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2855)
07-16 00:19:05.663: E/AndroidRuntime(29919): at android.app.ActivityThread.access$1700(ActivityThread.java:177)
07-16 00:19:05.663: E/AndroidRuntime(29919): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1520)
07-16 00:19:05.663: E/AndroidRuntime(29919): at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 00:19:05.663: E/AndroidRuntime(29919): at android.os.Looper.loop(Looper.java:145)
07-16 00:19:05.663: E/AndroidRuntime(29919): at android.app.ActivityThread.main(ActivityThread.java:5944)
07-16 00:19:05.663: E/AndroidRuntime(29919): at java.lang.reflect.Method.invoke(Native Method)
07-16 00:19:05.663: E/AndroidRuntime(29919): at java.lang.reflect.Method.invoke(Method.java:372)
07-16 00:19:05.663: E/AndroidRuntime(29919): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
07-16 00:19:05.663: E/AndroidRuntime(29919): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
Code:
private void gotoLocation(int id, double lat, double lng,
String route_direct) {
......
// Update the location.
for(Iterator<MapItem> it = mapItemList.iterator(); it.hasNext();){
//Line 132.
MapItem item1 = it.next();
if(item1.getId() == id){
marker = item1.getMarker();
marker.remove();
//int index = mapItemList.indexOf(item1);
it.remove();
ll = new LatLng(lat, lng);
MarkerOptions markerOpt = new MarkerOptions().title(
route_direct).position(ll);
marker = map.addMarker(markerOpt);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 11));
MapItem mapItem = new MapItem(id, marker);
mapItemList.add(mapItem);
}
}
}
HashMap<Integer, Marker>. Switching to using an ArrayList will just make it harder on you, as you can't do a lookup based on the key.