簡體   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?

我使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM