在这种方法中,我尝试遍历listSong以找到歌手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();
        }
    });
}


但这给了我这是错误的:

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)


循环后如何在适配器中添加listSong

最佳答案

例外应该是不言自明的:您试图同时修改listSong(在循环浏览列表本身时)。我无法告诉您正确的答案是什么,因为我不了解您要尝试做的事情的总体情况,但这通常不是您应该做的事情。

09-05 08:09