I have made a Song List using ArrayList, and I got all the information and it can be passed to the second activity and open it on a new page. What I want to have is when I click the song list, then it opens in a new page, and the new page shows the details and the image of the song.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView_Main=findViewById(R.id.listView_Main);
final Song song1 = new Song("Love Story","Taylor Swift","September 12,2008", "$1.29");
Song song2 = new Song("Lover","Taylor Swift","August 23,2019", "$1.29" );
Song song3 = new Song("I Forgot That You Existed", "Taylor Swift", "August 23,2019", "$1.29");
Song song4 = new Song("The Archer", "Taylor Swift", "August 23,2019", "$1.29");
Song song5 = new Song("I Think He Knows","Taylor Swift", "August 23,2019", "$1.29");
final List<Song> songs = new ArrayList<>();
songs.add(song1);
songs.add(song2);
songs.add(song3);
songs.add(song4);
songs.add(song5);
MainAdapter adapter = new MainAdapter(this, songs);
listView_Main.setAdapter(adapter);
listView_Main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent=new Intent(view.getContext(),SongDetailActivity.class);
intent.putExtra("currentSong",(Serializable) songs.get(i));
startActivity(intent);
}
});
}
But my problem is in the second activity, I don't know how to get the detail of the song, and the images by using the setText. So, I have made the song details in the String.xml file. How can I get those details and the image in the second activity? Also, I don't know how to get the position of the song list in the second activity, I mean the position of which song that I click, and It show the detail of that song I had clicked.
//Here is the second activity of the Song Details.//////
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song_detail);
textViewSongDetail=findViewById(R.id.textView_SongDetail);
imageViewSongDetail=findViewById(R.id.image_SongDetail);
Song currentSong=(Song)getIntent().getSerializableExtra("currentSong");
}
First of all make sure you've implemented Serializable in your model class Song. Than your code will work fine. And to receive the position you can send -
intent.putExtra("position", i); // to send
getIntent().getIntExtra("position", -1); //to receive (-1 is default value)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.