简体   繁体   English

包含自定义对象的Parcelable List

[英]Parcelable List that contains a custom object

I am trying to pass data ( ArrayList ) between Activities on Android 我试图在Android Activities之间传递数据( ArrayList

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? 当实现Parcelable的类不包含自定义对象(Shop)时,一切正常,但如果我的类包含自定义对象,我该怎么做?

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! 非常感谢你!

Your Shop class should implement Parcelable and you should use 您的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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM