Android 将数据从ListView传递到新活动

Android 将数据从ListView传递到新活动,android,Android,我正在开发一个音乐应用程序结构。我的编码经验很少。 我希望将listView中的数据(如标题和歌手姓名)发送到新活动。我知道我需要添加puExtra和getExtra方法,但是,我无法应用它 你能帮帮我吗? 谢谢 LatestSongActivity.java: package com.example.android.musicapp; import android.content.Intent; import android.support.v7.app.AppCompatActivity;

我正在开发一个音乐应用程序结构。我的编码经验很少。 我希望将listView中的数据(如标题和歌手姓名)发送到新活动。我知道我需要添加puExtra和getExtra方法,但是,我无法应用它

你能帮帮我吗? 谢谢

LatestSongActivity.java:

package com.example.android.musicapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LatestSongsActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_latest_songs);



        // Create a List of latest songs
        final ArrayList<Song> latestSongs = new ArrayList<Song>();

        latestSongs.add(new Song("Echame La Culpa", "Luis Fonsi,Demi Lovato"));
        latestSongs.add(new Song("Look What You Made Me Do", "Taylor Swift"));
        latestSongs.add(new Song("Too Good At Goodbyes", "Sam Smith"));
        latestSongs.add(new Song("No Tears Left To Cry", "ariana Grande"));
        latestSongs.add(new Song("This Is America", "Childish Gambino"));
        latestSongs.add(new Song("Sick Boy", "The Chainsmokers"));
        latestSongs.add(new Song("Bad At Love", "Halsey"));
        latestSongs.add(new Song("Never Be The Same", "Camila Cabello"));
        latestSongs.add(new Song("Havana", "Camila Cabello"));
        latestSongs.add(new Song("One Kiss", "Calvin Harris, Dua Lipa"));
        latestSongs.add(new Song("Back To You", "Selena Gomes"));
        latestSongs.add(new Song("Pray", "sam Smith"));
        latestSongs.add(new Song("Don't Say You Love Me", "Fifth Harmony"));


        final SongAdapter adapter =
                new SongAdapter(this, latestSongs);
        final ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                   int position, long id) {

                        Intent myIntent = new Intent(view.getContext(), PlaySong.class);
                startActivity(myIntent);

                    }

                });
        }
}
Play_song.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:scaleType="centerCrop"
        android:src="@drawable/play_image"/>

    <TextView
        android:id="@+id/song_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/artist_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/stop_icon"
            />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/play_icon" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/back_icon" />
    </LinearLayout>

</LinearLayout>

在单击时更改listview

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                               int position, long id) {

                      String title= latestSongs.get(position).getTitle();
                      String singer= latestSongs.get(position).getSinger();


            Intent myIntent = new Intent(view.getContext(), PlaySong.class);
            myIntent.putExtra("title", title);
            myIntent.putExtra("singer", singer);
            startActivity(myIntent);

                }

            });
    }

在LatestSongActivity.java中,将下面的代码放在调用PlaySong活动意图的位置

      Intent myIntent = new Intent(view.getContext(), PlaySong.class);
        myIntent.putExtra("list",latestSongs.get(position))
        startActivity(myIntent);
现在,您需要在歌曲类中扩展Parcebale。我已经更改了它,代码如下所示

    public class Song implements Parcelable {

  public Song() {

    }

        /** Title of the song */
        private String mTitle;

        /** Singer's name of the song */
        private String mSinger;

        public Song(String title, String singer) {
            mTitle = title;
            mSinger = singer;
        }

        protected Song(Parcel in) {
            mTitle = in.readString();
            mSinger = in.readString();
        }

        public static final Creator<Song> CREATOR = new Creator<Song>() {
            @Override
            public Song createFromParcel(Parcel in) {
                return new Song(in);
            }

            @Override
            public Song[] newArray(int size) {
                return new Song[size];
            }
        };

        /**
         Get title of the song.
         */
        public String getTitle() {
            return mTitle;
        }

        /**
         * Get the singer's name of the song.
         */
        public String getSinger() {
            return mSinger;

        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(mTitle);
            dest.writeString(mSinger);
        }
    }
 public class PlaySong extends AppCompatActivity {

        private Song song = new Song();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_play_song);
            song = getIntent().getParcelableExtra("list");

            // Perform your get methods from the class Song eg. 
            song.getSinger();

        }


    }

希望有帮助。

谢谢。不幸的是,仍然无法查看PlaySong活动中的数据
      Intent myIntent = new Intent(view.getContext(), PlaySong.class);
        myIntent.putExtra("list",latestSongs.get(position))
        startActivity(myIntent);
    public class Song implements Parcelable {

  public Song() {

    }

        /** Title of the song */
        private String mTitle;

        /** Singer's name of the song */
        private String mSinger;

        public Song(String title, String singer) {
            mTitle = title;
            mSinger = singer;
        }

        protected Song(Parcel in) {
            mTitle = in.readString();
            mSinger = in.readString();
        }

        public static final Creator<Song> CREATOR = new Creator<Song>() {
            @Override
            public Song createFromParcel(Parcel in) {
                return new Song(in);
            }

            @Override
            public Song[] newArray(int size) {
                return new Song[size];
            }
        };

        /**
         Get title of the song.
         */
        public String getTitle() {
            return mTitle;
        }

        /**
         * Get the singer's name of the song.
         */
        public String getSinger() {
            return mSinger;

        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(mTitle);
            dest.writeString(mSinger);
        }
    }
 public class PlaySong extends AppCompatActivity {

        private Song song = new Song();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_play_song);
            song = getIntent().getParcelableExtra("list");

            // Perform your get methods from the class Song eg. 
            song.getSinger();

        }


    }