madcoderz
madcoderz

Reputation: 4431

parcelable is not working android

I'm trying to send an ArrayList of custom objects to the next activity but its not working for me. I've been all day trying to implement Parcelable with no luck at all. I've done my research here on SO but i'm still getting an error: Cannot convert Parceable[] to ArraList<Song> This is my code for parceling my object:

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

    } 

In my first activity i just:

ArrayList<Song> songs = db.selectAllSongs();
playerActivity.putParcelableArrayListExtra("songs", songs);

And in my second activity i just:

ArrayList<Song> songs = getIntent().getParcelableArrayExtra("songs");

I don't know why im getting an error when i'm implementing Parceable. I need a little guiding. Thanks in advance.

Upvotes: 0

Views: 1320

Answers (2)

Bebin T.N
Bebin T.N

Reputation: 2649

I hope this will help for you. In this way i am using parcellable values

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

}}

And in Activity class

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

And in the second Activity screen get the bundle parcelable array like this

Bundle getBundle = this.getIntent().getExtras();
List<Channel> channelsList = getBundle.getParcelableArrayList("channel");

Upvotes: 1

Collin Flynn
Collin Flynn

Reputation: 771

The method Intent.getParcelableArrayExtra() (link) returns an array of parcelable objects. You're assigning that to an ArrayList object, and the compiler is complaining because ArrayLists aren't arrays.

Try using getParcelableArrayListExtra() instead.

Upvotes: 0

Related Questions