我正在尝试在ArrayList
上的Activities
之间传递数据(Android
)
当实现Parcelable
的类不包含自定义对象(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];
}
};
这是我的另一个类商品,它包含商店的对象
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);
}
我应该读写哪种数据类型?
非常感谢!
答案 0 :(得分:12)
您的Shop
课程应该实施Parcelable
,您应该使用
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);
}