![](/img/trans.png)
[英]How to set an intent so that I can send a data to the second activity and from there to the third activity?
[英]When I intent the arrayList's data, and how can I get those data in the second activity and setText and Image?
我使用 ArrayList 制作了一个歌曲列表,我得到了所有信息,可以将其传递给第二个活动并在新页面上打开它。 我想要的是当我单击歌曲列表时,它会在新页面中打开,新页面显示歌曲的详细信息和图像。
@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);
}
});
}
但我的问题是在第二个活动中,我不知道如何使用 setText 获取歌曲的细节和图像。 因此,我在 String.xml 文件中制作了歌曲的详细信息。 如何在第二个活动中获取这些详细信息和图像? 另外,我不知道如何获得第二个活动中歌曲列表的position,我的意思是我点击了哪首歌的position,它显示了我点击的那首歌的详细信息。
//这里是歌曲详情的第二个活动。//////
@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");
}
首先确保您已经在 model class Song 中实现了 Serializable。 比您的代码可以正常工作。 要接收 position,您可以发送 -
intent.putExtra("position", i); // to send
getIntent().getIntExtra("position", -1); //to receive (-1 is default value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.