我正在尝试将自定义对象的 ArrayList 发送到下一个活动,但它对我不起作用。我整天都在尝试实现 Parcelable,但一点运气都没有。我已经在这里完成了关于 SO 的研究,但我仍然遇到错误:Cannot convert Parceable[] to ArraList<Song>
这是我用于打包对象的代码:
// Parcelling part
public Song(Parcel in){
this._id = in.readInt();
this.name = in.readString();
this.path = in.readString();
this.artistId = in.readInt();
this.albumId = in.readInt();
}
@Override
public int describeContents(){
return 0;
}
public static final Parcelable.Creator<Song> CREATOR = new Parcelable.Creator<Song>() {
public Song createFromParcel(Parcel in) {
return new Song(in);
}
public Song[] newArray(int size) {
return new Song[size];
}
};
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(_id);
dest.writeString(name);
dest.writeString(path);
dest.writeInt(artistId);
dest.writeInt(albumId);
}
在我的第一个活动中,我只是:
ArrayList<Song> songs = db.selectAllSongs();
playerActivity.putParcelableArrayListExtra("songs", songs);
在我的第二个活动中,我只是:
ArrayList<Song> songs = getIntent().getParcelableArrayExtra("songs");
我不知道为什么我在实现 Parceable 时会出错。我需要一点指导。提前致谢。