1

In this method, I try to loop over listSong to find the singer ID:

private void getAllSongBySinger() {
    SongService songService = ServiceGenerator.createService(SongService.class);
    Call<SongRespones> callAllSong = songService.findAllSongBySinger();
    callAllSong.enqueue(new Callback<SongRespones>() {
        @Override
        public void onResponse(Call<SongRespones> call, Response<SongRespones> response) {
            listSong = new ArrayList<>();
            songRespones = response.body();
            listSong = songRespones.getSongs();
            for (SongRespones.Songs songs : listSong) {
                if (songs.getSinger().getId() == getSingerId) {
                    listSong.add(songs);
                    adapter.addMoreItem(listSong);
                    rvSongBySinger.setAdapter(adapter);  
                }
            }
        }
        @Override
        public void onFailure(Call<SongRespones> call, Throwable t) {
            t.printStackTrace();
        }
    });
}

But it gives me this is error:

08-11 18:44:54.852 2280-2280/com.vichit.khmersong E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vichit.khmersong, PID: 2280
java.util.ConcurrentModificationException
    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
    at com.vichit.khmersong.fragment.main.DetailSingerSongFragment$1.onResponse(DetailSingerSongFragment.java:76)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

How can I add listSong to adapter after the loop?

Helder Pereira
  • 5,522
  • 2
  • 35
  • 52
Vichit
  • 309
  • 1
  • 3
  • 16
  • Possible duplicate of [Why ConcurrentModificationException in ArrayList?](https://stackoverflow.com/questions/12793199/why-concurrentmodificationexception-in-arraylist) – Sufian Aug 17 '17 at 20:38

1 Answers1

-1

The exception should be self-explanatory: you are trying to modify listSong concurrently (while you are looping through the list itself). I can't tell you what the right answer is because I don't know the big picture of what you are attempting to do, but this is generally not something you should be doing.

Roney Michael
  • 3,964
  • 5
  • 30
  • 45