简体   繁体   English

当我打算获取 arrayList 的数据时,如何在第二个活动和 setText 和 Image 中获取这些数据?

[英]When I intent the arrayList's data, and how can I get those data in the second activity and setText and Image?

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.我使用 ArrayList 制作了一个歌曲列表,我得到了所有信息,可以将其传递给第二个活动并在新页面上打开它。 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.但我的问题是在第二个活动中,我不知道如何使用 setText 获取歌曲的细节和图像。 So, I have made the song details in the String.xml file.因此,我在 String.xml 文件中制作了歌曲的详细信息。 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.另外,我不知道如何获得第二个活动中歌曲列表的position,我的意思是我点击了哪首歌的position,它显示了我点击的那首歌的详细信息。

//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.首先确保您已经在 model class Song 中实现了 Serializable。 Than your code will work fine.比您的代码可以正常工作。 And to receive the position you can send -要接收 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM