java - 单击另一个活动时如何发送自定义arraylist的列表项的数据?
问题描述
我有一个自定义歌曲对象的数组列表,当单击列表视图的特定歌曲项时,我想将歌曲名称、艺术家姓名和专辑封面图像发送到另一个活动(歌曲活动)。我创建了一个 SongActivity 来显示用户选择的歌曲的正在播放屏幕。
(歌曲列表活动)这包含歌曲列表。
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)当用户点击一个特定的歌曲对象时,这个活动开始
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);
}
}
然后设置并获取使用意图
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, studentList)
intents.getParcelableArrayList<Student>(Intent.EXTRA_STREAM)
推荐阅读
- amazon-web-services - docker-compose 没有这样的命令:转换错误
- mqtt - 使用 Mosquitto brokier 从多个服务器接收 MQTT
- kubernetes - 如何将部署分配给特定的节点池
- angular - 从内部订阅添加角度组件输入数据
- swift - hashable hashValue to hash(into:) for a CGPoint
- python - Pandas Dataframe 如何通过逗号将包含不同数量元素的列拆分为行
- html - 垂直居中子元素,同时填充父 div 的全高
- php - CRITICAL messenger 处理消息 AppBundle\Message\Message 时引发错误
- python - 使用 GitHub 解码器脚本解码有效负载
- javascript - for 循环中的 setTimeout() 函数出错