Android将循环后的所有对象添加到列表中

时间:2017-08-12 09:48:40

标签: java android retrofit2

我在完成循环后尝试将所有对象添加到列表中。 这是我想要添加的适配器中的方法。

public MusicCustomAdapter(Context context) {
            songList = new ArrayList<>();
            this.context = context;
}

public void addMoreItem(List<SongRespones.Songs> songList) {
                this.songList = songList;
                notifyDataSetChanged();
 }

这是我使用Retrofit的callResponse:

    private void getAllSongBySinger() {
            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) {
                    List<SongRespones.Songs> listSong;
                    SongRespones songRespones = response.body();
                    listSong = songRespones.getSongs();
                    for (SongRespones.Songs songs : listSong) {
                        if (songs.getSinger().getId() == getSingerId) {
                            Log.e(LOG, songs + "");
                            adapter.addMoreItem(What must I input in here?);
                            recyclerView.setAdapter(adapter);

                        }
                    }

                }

                @Override
                public void onFailure(Call<SongRespones> call, Throwable t) {
                    t.printStackTrace();
                }
            });

        }

请帮帮我:

1 个答案:

答案 0 :(得分:1)

我假设您只想展示选定的歌手的歌曲。所以基本上在你的onResponse方法中创建新列表并添加你想要显示的歌曲,最后用这个列表更新你的适配器。

                SongRespones songRespones = response.body();
                List<SongRespones.Songs> listSong = songRespones.getSongs();
                List<SongRespones.Songs> selectedSingersSongs = new Arraylist();
                for (SongRespones.Songs songs : listSong) {
                    if (songs.getSinger().getId() == getSingerId) {
                        Log.e(LOG, songs + "");
                        selectedSingersSongs.add(songs);
                    }
                }                    
                adapter.addMoreItem(selectedSingersSongs);

当然你应该recyclerView.setAdapter(adapter);之前