简体   繁体   中英

Passing data from ListView to New Activity

I am working on a Musical app structure. My experience with coding is very little. I would love to send data (such as title and singer name) from the listView to a new activity. I understand I need to add puExtra and getExtra methods, however, I am not able to apply it.

Could you please help me? Thanks

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);

                    }

                });
        }
}

Song.java:

public class 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;
        }
        /**
         Get title of the song.
         */
        public String getTitle() {
            return mTitle;
        }

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

        }
    }

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>

PlaySong.java

public class PlaySong extends AppCompatActivity {


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


    }


        }

Change your listview onclick,

 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);

                }

            });
    }

and get data in another activity like this in onCreate();

    Intent in      = getIntent();
    String title = in.getStringExtra("title");
    String singer = in.getStringExtra("singer");

In your, LatestSongActivity.java put the below code where you have called the Intent for the PlaySong activity.

      Intent myIntent = new Intent(view.getContext(), PlaySong.class);
        myIntent.putExtra("list",latestSongs.get(position))
        startActivity(myIntent);

Now you need to extend Parcebale in your Song class. I have changed it and the code is shown below.

    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);
        }
    }

Now in the PlaySong Activity where you need to get the song data you need to modify the activity code. Activity code is shown below.

 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();

        }


    }

Hope it Helps.

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.

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