gpt4 book ai didi

java - 在 Android 上将项目添加到适配器时出现 ConcurrentModificationException

转载 作者:太空宇宙 更新时间:2023-11-04 11:14:26 24 4
gpt4 key购买 nike

在此方法中,我尝试循环 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 (当您循环遍历列表本身时)。我无法告诉您正确的答案是什么,因为我不知道您正在尝试做什么的总体情况,但这通常不是您应该做的事情。

关于java - 在 Android 上将项目添加到适配器时出现 ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45644809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com