我正在尝试将自定义对象的ArrayList发送到下一个活动,但它不适用于我。我整天都试图实现Parcelable而没有运气。我在这里完成了我的研究,但我仍然收到错误: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时会出错。我需要一点指导。提前谢谢。
答案 0 :(得分:1)
我希望这会对你有所帮助。通过这种方式,我使用parcellable值
public class Channel implements Serializable, Parcelable {
/** */
private static final long serialVersionUID = 4861597073026532544L;
private String cid;
private String uniqueID;
private String name;
private String logo;
/**
* @return the cid
*/
public String getCid() {
return cid;
}
/**
* @param cid
* the cid to set
*/
public void setCid(String cid) {
this.cid = cid;
}
/**
* @return the uniqueID
*/
public String getUniqueID() {
return uniqueID;
}
/**
* @param uniqueID
* the uniqueID to set
*/
public void setUniqueID(String uniqueID) {
this.uniqueID = uniqueID;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the logo
*/
public String getLogo() {
return logo;
}
/**
* @param logo
* the logo to set
*/
public void setLogo(String logo) {
this.logo = logo;
}
public Channel(Parcel in) {
super();
readFromParcel(in);
}
public static final Parcelable.Creator<Channel> CREATOR = new Parcelable.Creator<Channel>() {
public Channel createFromParcel(Parcel in) {
return new Channel(in);
}
public Channel[] newArray(int size) {
return new Channel[size];
}
};
public void readFromParcel(Parcel in) {
String[] result = new String[23];
in.readStringArray(result);
this.cid = result[0];
this.uniqueID = result[1];
this.name = result[2];
this.logo = result[3];
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] { this.cid, this.uniqueID,
this.name, this.logo});
}}
并在Activity类
中bundle.putParcelableArrayList("channel",
(ArrayList<Channel>) channels);
在第二个“活动”屏幕中,获取此类
的束可分割数组Bundle getBundle = this.getIntent().getExtras();
List<Channel> channelsList = getBundle.getParcelableArrayList("channel");
答案 1 :(得分:0)
方法Intent.getParcelableArrayExtra()
(link)返回一个parcelable对象数组。您将其分配给ArrayList对象,并且编译器抱怨,因为ArrayList
不是数组。
请尝试使用getParcelableArrayListExtra()
。