0

我正在尝试将自定义对象的 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 时会出错。我需要一点指导。提前致谢。

4

2 回答 2

1

我希望这对你有帮助。通过这种方式,我正在使用可分割的值

    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});

}}

在活动课上

bundle.putParcelableArrayList("channel",
                        (ArrayList<Channel>) channels);

在第二个 Activity 屏幕中,像这样获取 bundle parcelable 数组

Bundle getBundle = this.getIntent().getExtras();
List<Channel> channelsList = getBundle.getParcelableArrayList("channel");
于 2013-08-09T17:08:49.243 回答
0

方法Intent.getParcelableArrayExtra() (link)返回一个可打包对象的数组。您将其分配给 ArrayList 对象,编译器抱怨,因为ArrayLists 不是数组。

尝试getParcelableArrayListExtra()改用。

于 2013-08-09T16:09:25.920 回答