Before adding works fine with the normal for each loop, but removing isn't so I used an iterator. Now every 3rd item I add to my arrayList gives me a ConcurrentModificationException. What seems to be the problem?
private ArrayList<Marker> mMarkers which is instantiated in onCreate
Here I have a list view on my onCreateView
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
final Events event = mEventsParseQueryAdapter.getItem(position);
GoogleMap googleMap = mSupportMapFragment.getMap();
if (mMarkers.isEmpty()) {
mMarkers.add(googleMap.addMarker(new MarkerOptions()
.position(getLatLngFromGeoPoint(event.getLocation()))));
} else {
Iterator<Marker> markerIterator = mMarkers.iterator();
//noinspection WhileLoopReplaceableByForEach
while (markerIterator.hasNext()) {
Marker marker = markerIterator.next();
if (marker.getPosition().equals(getLatLngFromGeoPoint(event.getLocation()))) {
return false;
} else {
mMarkers.add(googleMap.addMarker(new MarkerOptions()
.position(getLatLngFromGeoPoint(event.getLocation()))));
}
}
}
return true;
}
});
and here's my marker on click listener which pops a dialog and have an option to remove the marker
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(marker.getPosition(), CONSTANT.ZOOM_LEVEL));
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
dialog.setNegativeButton(R.string.checkout, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mMarkers.remove(marker);
marker.remove();
}
}).setPositiveButton(R.string.frames, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.create().show();
return true;
}
});