我正在开发音乐应用程序结构。我的编码经验很少。 我很乐意将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);
}
});
}
}
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);
}
}
答案 0 :(得分:0)
更改列表视图,点击
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);
}
});
}
并在onCreate();
中获取此类其他活动中的数据 Intent in = getIntent();
String title = in.getStringExtra("title");
String singer = in.getStringExtra("singer");
答案 1 :(得分:0)
在您的,LatestSongActivity.java中,将以下代码放在您为PlaySong活动调用Intent的位置。
Intent myIntent = new Intent(view.getContext(), PlaySong.class);
myIntent.putExtra("list",latestSongs.get(position))
startActivity(myIntent);
现在您需要在Song类中扩展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);
}
}
现在,在需要获取歌曲数据的PlaySong活动中,您需要修改活动代码。活动代码如下所示。
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();
}
}
希望它有所帮助。