0

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

3 Answers 3

2

You cannot modify a list while iterating over it, no matter whether you use for-each or an iterator. Read more on the exception page: http://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html

Sign up to request clarification or add additional context in comments.

Comments

1
mMarkers.add(googleMap.addMarker(new MarkerOptions()

is called inside your iteration loop (while (markerIterator.hasNext())), so you are adding element at the time of traversing the collection which gives you the error.

Actually you probably wanted the :

           } else {
                mMarkers.add(googleMap.addMarker(new MarkerOptions()
                        .position(getLatLngFromGeoPoint(event.getLocation()))));
            }

branch not inside the loop but rahter outside the whole while loop (when no existing marker was found a new one is created and added.

Comments

0

You are parsing/adding or reading/writing on the list concurrently using a fail-fast iterator(markerIterator in your casE). If the iterator will detect such behavior in the code it will throw this exception.

it is not generally permissible for one thread to modify a Collection while another thread is iterating over it

Hence your read/write operations should be disassociated, time vise.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.