Reputation: 1031
I am trying to pass data (ArrayList
) between Activities
on Android
All works fine when the class that implements Parcelable
doesn´t contains custom objects (Shop), but what do I have to do if my class contains one?
Shop
public Shop(Parcel in) {
this.id = in.readString();
this.name = in.readString();
this.type = in.readString();
this.lat= in.readDouble();
this.long= in.readDouble();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
dest.writeString(this.name);
dest.writeString(this.type);
dest.writeDouble(this.lat);
dest.writeDouble(this.long);
}
@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Shop createFromParcel(Parcel in) {
return new Shop(in);
}
public Shop[] newArray(int size) {
return new Shop[size];
}
};
And here is my other class Offer which is the one that contains an object of Shop
public Offer(Parcel in) {
this.id = in.readInt();
this.title = in.readString();
this.myShop = in.read.......();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.title);
dest.write......(this.myShop);
}
Which data type should I read and write ?
Thank you very much!
Upvotes: 3
Views: 5675
Reputation: 1843
Your Shop
class should implement Parcelable
and you should use
public Offer(Parcel in) {
this.id = in.readInt();
this.title = in.readString();
this.myShop = in.readParcelable(Shop.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.title);
dest.writeParcelable(this.myShop, flags);
}
Upvotes: 15