如何在单击时将自定义数组列表的列表项的数据发送到另一个 activity?
How to send data of the list item of a custom arraylist when clicked to another activity?
我有一个自定义歌曲对象的数组列表,我想在特定歌曲项目时将歌曲名称、艺术家姓名和专辑封面图像发送到另一个 activity(歌曲 Activity)列表视图的被点击。我创建了一个 SongActivity 来显示用户选择的歌曲的正在播放屏幕。
(SONGS LIST ACTIVITY) 这包含歌曲列表。
public class SongsListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_list);
ArrayList<Song> songs = new ArrayList<Song>();
songs.add(new Song("Earthquake","Marshmello and TYNAN",R.mipmap.earthquake));
.....
final SongAdapter adapter = new SongAdapter(this, songs);
ListView listView = findViewById(R.id.list);
listView.setAdapter(adapter);
ListView listview = (ListView) findViewById(R.id.list);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(view.getContext(), SongActivity.class);
}
});
}
}
(SONG ACTIVITY) 此 activity 在用户单击特定歌曲对象时启动
import androidx.appcompat.app.AppCompatActivity;
public class SongActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song_activity);
ImageView songImage = findViewById(R.id.songImage);
TextView songName = findViewById(R.id.songName);
TextView artistName = findViewById(R.id.artistName);
Intent intent = getIntent();
songImage.setImageResource(intent.getIntExtra("image",0));
songName.setText(intent.getStringExtra("songName"));
artistName.setText(intent.getStringExtra("artistName"));
}
}
(歌曲适配器)
public class SongAdapter extends ArrayAdapter {
public SongAdapter(Activity context, ArrayList<Song> songs) {
super(context, 0, songs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Song currentSong = (Song) getItem(position);
TextView nameTextView = (TextView) listItemView.findViewById(R.id.song_name);
nameTextView.setText(currentSong.getSongName());
TextView artistTextView = (TextView) listItemView.findViewById(R.id.artist_name);
artistTextView.setText(currentSong.getArtistName());
ImageView iconView = (ImageView) listItemView.findViewById(R.id.song_icon);
iconView.setImageResource(currentSong.getImageResourceId());
return listItemView;
}
}
像这样Paracelize你的模态
public class Student implements Parcelable {
private Integer rollno;
private String name;
private Integer age;
protected Student(Parcel in) {
age = in.readInt();
name = in.readString();
rollno = in.readInt();
}
public Student(Integer age, String name, Integer rollno) {
this.age = age;
this.name = name;
this.rollno = rollno;
}
public static final Creator<Student> CREATOR = new Creator<Student>() {
@Override
public Student createFromParcel(Parcel in) {
return new Student(in);
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
@Override
public int describeContents() {
return 0;
}
public Integer getRollno() {
return rollno;
}
public void setRollno(Integer rollno) {
this.rollno = rollno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public static Creator<Student> getCREATOR() {
return CREATOR;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(age);
parcel.writeString(name);
parcel.writeInt(rollno);
}
}
And then set and get using intent
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, studentList)
intents.getParcelableArrayList<Student>(Intent.EXTRA_STREAM)
你的歌曲模型(Class)应该是这样的
import android.os.Parcel;
import android.os.Parcelable;
public final class Songs implements Parcelable {
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
@Override
public Songs createFromParcel(final Parcel source) {
return new Songs(source);
}
@Override
public Songs[] newArray(final int size) {
return new Songs[size];
}
};
private String songName;
private String artistName;
private Integer imageId;
public Songs() {
}
public Songs(final String songName, final String artistName, final Integer imageId) {
this.songName = songName;
this.artistName = artistName;
this.imageId = imageId;
}
protected Songs(final Parcel in) {
this.songName = in.readString();
this.artistName = in.readString();
this.imageId = (Integer) in.readValue(Integer.class.getClassLoader());
}
public String getSongName() {
return songName;
}
public void setSongName(final String songName) {
this.songName = songName;
}
public String getArtistName() {
return artistName;
}
public void setArtistName(final String artistName) {
this.artistName = artistName;
}
public Integer getImageId() {
return imageId;
}
public void setImageId(final Integer imageId) {
this.imageId = imageId;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(final Parcel dest, final int flags) {
dest.writeString(this.songName);
dest.writeString(this.artistName);
dest.writeValue(this.imageId);
}
}
在你的SongListActivity
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position, long id) {
Intent intent = new Intent(view.getContext(), SongActivity.class);
intent.putExtra("SONG_DATA", songList.get(position));
startActivity(intent);
}
});
最后,在您 SongActivity
的 onCreate()
中,您必须了解意图并获取像
这样的数据
if (getIntent() != null) {
Song song = getIntent().getParcelableExtra("SONG_DATA");
// now you have got song object, You can do rest of operations
}
我有一个自定义歌曲对象的数组列表,我想在特定歌曲项目时将歌曲名称、艺术家姓名和专辑封面图像发送到另一个 activity(歌曲 Activity)列表视图的被点击。我创建了一个 SongActivity 来显示用户选择的歌曲的正在播放屏幕。
(SONGS LIST ACTIVITY) 这包含歌曲列表。
public class SongsListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_list);
ArrayList<Song> songs = new ArrayList<Song>();
songs.add(new Song("Earthquake","Marshmello and TYNAN",R.mipmap.earthquake));
.....
final SongAdapter adapter = new SongAdapter(this, songs);
ListView listView = findViewById(R.id.list);
listView.setAdapter(adapter);
ListView listview = (ListView) findViewById(R.id.list);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(view.getContext(), SongActivity.class);
}
});
}
}
(SONG ACTIVITY) 此 activity 在用户单击特定歌曲对象时启动
import androidx.appcompat.app.AppCompatActivity;
public class SongActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song_activity);
ImageView songImage = findViewById(R.id.songImage);
TextView songName = findViewById(R.id.songName);
TextView artistName = findViewById(R.id.artistName);
Intent intent = getIntent();
songImage.setImageResource(intent.getIntExtra("image",0));
songName.setText(intent.getStringExtra("songName"));
artistName.setText(intent.getStringExtra("artistName"));
}
}
(歌曲适配器)
public class SongAdapter extends ArrayAdapter {
public SongAdapter(Activity context, ArrayList<Song> songs) {
super(context, 0, songs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Song currentSong = (Song) getItem(position);
TextView nameTextView = (TextView) listItemView.findViewById(R.id.song_name);
nameTextView.setText(currentSong.getSongName());
TextView artistTextView = (TextView) listItemView.findViewById(R.id.artist_name);
artistTextView.setText(currentSong.getArtistName());
ImageView iconView = (ImageView) listItemView.findViewById(R.id.song_icon);
iconView.setImageResource(currentSong.getImageResourceId());
return listItemView;
}
}
像这样Paracelize你的模态
public class Student implements Parcelable {
private Integer rollno;
private String name;
private Integer age;
protected Student(Parcel in) {
age = in.readInt();
name = in.readString();
rollno = in.readInt();
}
public Student(Integer age, String name, Integer rollno) {
this.age = age;
this.name = name;
this.rollno = rollno;
}
public static final Creator<Student> CREATOR = new Creator<Student>() {
@Override
public Student createFromParcel(Parcel in) {
return new Student(in);
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
@Override
public int describeContents() {
return 0;
}
public Integer getRollno() {
return rollno;
}
public void setRollno(Integer rollno) {
this.rollno = rollno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public static Creator<Student> getCREATOR() {
return CREATOR;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(age);
parcel.writeString(name);
parcel.writeInt(rollno);
}
}
And then set and get using intent
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, studentList)
intents.getParcelableArrayList<Student>(Intent.EXTRA_STREAM)
你的歌曲模型(Class)应该是这样的
import android.os.Parcel; import android.os.Parcelable; public final class Songs implements Parcelable { public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { @Override public Songs createFromParcel(final Parcel source) { return new Songs(source); } @Override public Songs[] newArray(final int size) { return new Songs[size]; } }; private String songName; private String artistName; private Integer imageId; public Songs() { } public Songs(final String songName, final String artistName, final Integer imageId) { this.songName = songName; this.artistName = artistName; this.imageId = imageId; } protected Songs(final Parcel in) { this.songName = in.readString(); this.artistName = in.readString(); this.imageId = (Integer) in.readValue(Integer.class.getClassLoader()); } public String getSongName() { return songName; } public void setSongName(final String songName) { this.songName = songName; } public String getArtistName() { return artistName; } public void setArtistName(final String artistName) { this.artistName = artistName; } public Integer getImageId() { return imageId; } public void setImageId(final Integer imageId) { this.imageId = imageId; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(final Parcel dest, final int flags) { dest.writeString(this.songName); dest.writeString(this.artistName); dest.writeValue(this.imageId); } }
在你的SongListActivity
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) { Intent intent = new Intent(view.getContext(), SongActivity.class); intent.putExtra("SONG_DATA", songList.get(position)); startActivity(intent); } });
最后,在您 SongActivity
的 onCreate()
中,您必须了解意图并获取像
if (getIntent() != null) {
Song song = getIntent().getParcelableExtra("SONG_DATA");
// now you have got song object, You can do rest of operations
}