Search code examples
javaandroidandroid-activityandroid-listviewonclicklistener

How to send data of the list item of a custom arraylist when clicked to another activity?


I have an array list of custom Song objects and I want to send the song name, artist name and the album cover image to another activity (Song Activity) when a particular song item of the listview is clicked. I have created a SongActivity for showing the now playing screen for the song that the user has selected.

(SONGS LIST ACTIVITY) This contains the songs list.

 public class SongsListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.songs_list);
    ArrayList<Song> songs = new ArrayList<Song>();
            songs.add(new Song("Earthquake","Marshmello and TYNAN",R.mipmap.earthquake));
        .....

            final SongAdapter adapter = new SongAdapter(this, songs);


            ListView listView = findViewById(R.id.list);

            listView.setAdapter(adapter);

            ListView listview = (ListView) findViewById(R.id.list);
            listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Intent intent = new Intent(view.getContext(), SongActivity.class);

                }
            });

        }
    }

(SONG ACTIVITY) This activity starts when a particular song object is clicked by the user

import androidx.appcompat.app.AppCompatActivity;

public class SongActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.song_activity);
        ImageView songImage = findViewById(R.id.songImage);
        TextView songName = findViewById(R.id.songName);
        TextView artistName = findViewById(R.id.artistName);

        Intent intent = getIntent();

        songImage.setImageResource(intent.getIntExtra("image",0));
        songName.setText(intent.getStringExtra("songName"));
        artistName.setText(intent.getStringExtra("artistName"));

    }
}

(SONG ADAPTER)

public class SongAdapter extends ArrayAdapter {

    public SongAdapter(Activity context, ArrayList<Song> songs) {

        super(context, 0, songs);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if(listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }


        Song currentSong = (Song) getItem(position);

        TextView nameTextView = (TextView) listItemView.findViewById(R.id.song_name);

        nameTextView.setText(currentSong.getSongName());

        TextView artistTextView = (TextView) listItemView.findViewById(R.id.artist_name);

        artistTextView.setText(currentSong.getArtistName());

        ImageView iconView = (ImageView) listItemView.findViewById(R.id.song_icon);

        iconView.setImageResource(currentSong.getImageResourceId());

        return listItemView;
    }

}

Solution

  • Your Song Model(Class) Should look like this

    
        import android.os.Parcel;
        import android.os.Parcelable;
    
        public final class Songs implements Parcelable {
            public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
                @Override
                public Songs createFromParcel(final Parcel source) {
                    return new Songs(source);
                }
    
                @Override
                public Songs[] newArray(final int size) {
                    return new Songs[size];
                }
            };
            private String songName;
            private String artistName;
            private Integer imageId;
    
            public Songs() {
            }
    
            public Songs(final String songName, final String artistName, final Integer imageId) {
                this.songName = songName;
                this.artistName = artistName;
                this.imageId = imageId;
            }
    
            protected Songs(final Parcel in) {
                this.songName = in.readString();
                this.artistName = in.readString();
                this.imageId = (Integer) in.readValue(Integer.class.getClassLoader());
            }
    
            public String getSongName() {
                return songName;
            }
    
            public void setSongName(final String songName) {
                this.songName = songName;
            }
    
            public String getArtistName() {
                return artistName;
            }
    
            public void setArtistName(final String artistName) {
                this.artistName = artistName;
            }
    
            public Integer getImageId() {
                return imageId;
            }
    
            public void setImageId(final Integer imageId) {
                this.imageId = imageId;
            }
    
            @Override
            public int describeContents() {
                return 0;
            }
    
            @Override
            public void writeToParcel(final Parcel dest, final int flags) {
                dest.writeString(this.songName);
                dest.writeString(this.artistName);
                dest.writeValue(this.imageId);
            }
        }
    
    

    In your SongListActivity

    
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView parent, View view, int position, long id) {
    
                        Intent intent = new Intent(view.getContext(), SongActivity.class);
                        intent.putExtra("SONG_DATA", songList.get(position));
                        startActivity(intent);
                    }
                });
    
    

    And at last in onCreate() of your SongActivity you have to get the intent and get the data like

    if (getIntent() != null) {
        Song song = getIntent().getParcelableExtra("SONG_DATA");
        // now you have got song object, You can do rest of operations
    }