Vichit
Vichit

Reputation: 339

Android add all object to list after loop

I try to add all object to list after I finish the loop. Here is my method in adapter that I want to add.

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

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

This is my callResponse using Retrofit:

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

        }

Please help me:

Upvotes: 0

Views: 674

Answers (1)

Fatih Santalu
Fatih Santalu

Reputation: 4701

I assume you only want to show selected singer's song. So basically in your onResponse method create new list and add the songs you want to show and finally update your adapter with this list.

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

And of course you should recyclerView.setAdapter(adapter); before

Upvotes: 1

Related Questions