简体   繁体   English

如何用一个对象包裹一个类?

[英]How to parcelable a class with an object inside?

I want to make a parcelable class, but one of the variables is another object, so I don't know how to manage this. 我想创建一个可拆分的类,但是变量之一是另一个对象,所以我不知道如何管理它。

public class CloseItPending implements Parcelable {

    @Key
    int id;

    @Key
    CategoryDate date;

    @Key
    String service;

    @Key
    String characteristics;

    @Key
    String buydate;

    @Key
    String payingmethod;

    @Key
    String price;

    @Key
    String others;

    @Key
    int servicecompanychange;

    @Key
    int characteristicscompanychange;

    @Key
    int buydatecompanychange;

    @Key
    int payingmethodcompanychange;

    @Key
    int pricecompanychange;

    @Key
    int otherscompanychange;

    @Key
    int validate_user;

    @Key
    int validate_company;

    ...

As you can see, the second variable which is named date and it type is CategoryDate. 如您所见,第二个变量名为date,类型为CategoryDate。

How must be the next methods? 接下来的方法应该如何?

public int describeContents()

public void writeToParcel(Parcel dest, int flags)

Thanks for your help. 谢谢你的帮助。

POSSIBILITY: 可能性:

public class CloseItPending implements Parcelable {

    @Key
    int id;

    @Key
    CategoryDate date;

    @Key
    String service;

    @Key
    String characteristics;

    @Key
    String buydate;

    @Key
    String payingmethod;

    @Key
    String price;

    @Key
    String others;

    @Key
    int servicecompanychange;

    @Key
    int characteristicscompanychange;

    @Key
    int buydatecompanychange;

    @Key
    int payingmethodcompanychange;

    @Key
    int pricecompanychange;

    @Key
    int otherscompanychange;

    @Key
    int validate_user;

    @Key
    int validate_company;

    public CloseItPending() {
    }

    public CloseItPending(Parcel source){
        readFromParcel(source);
    }

    public CloseItPending(int id, CategoryDate date, String service, String characteristics, String buydate, String payingmethod, String price, String others, int servicecompanychange, int characteristicscompanychange, int buydatecompanychange, int payingmethodcompanychange, int pricecompanychange, int otherscompanychange, int validate_user, int validate_company) {
        this.id = id;
        this.date = date;
        this.service = service;
        this.characteristics = characteristics;
        this.buydate = buydate;
        this.payingmethod = payingmethod;
        this.price = price;
        this.others = others;
        this.servicecompanychange = servicecompanychange;
        this.characteristicscompanychange = characteristicscompanychange;
        this.buydatecompanychange = buydatecompanychange;
        this.payingmethodcompanychange = payingmethodcompanychange;
        this.pricecompanychange = pricecompanychange;
        this.otherscompanychange = otherscompanychange;
        this.validate_user = validate_user;
        this.validate_company = validate_company;
    }

    private void readFromParcel(Parcel in) {
        this.id = in.readInt();

        //Object parcelable too
        this.date = in.readParcelable(CategoryDate.class.getClassLoader());

        this.service = in.readString();
        this.characteristics = in.readString();
        this.buydate = in.readString();
        this.payingmethod = in.readString();
        this.price = in.readString();
        this.others = in.readString();
        this.servicecompanychange = in.readInt();
        this.characteristicscompanychange = in.readInt();
        this.buydatecompanychange = in.readInt();
        this.payingmethodcompanychange = in.readInt();
        this.pricecompanychange = in.readInt();
        this.otherscompanychange = in.readInt();
        this.validate_user = in.readInt();
        this.validate_company = in.readInt();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public CategoryDate getDate() {
        return date;
    }

    public void setDate(CategoryDate date) {
        this.date = date;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String getCharacteristics() {
        return characteristics;
    }

    public void setCharacteristics(String characteristics) {
        this.characteristics = characteristics;
    }

    public String getBuydate() {
        return buydate;
    }

    public void setBuydate(String buydate) {
        this.buydate = buydate;
    }

    public String getPayingmethod() {
        return payingmethod;
    }

    public void setPayingmethod(String payingmethod) {
        this.payingmethod = payingmethod;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getOthers() {
        return others;
    }

    public void setOthers(String others) {
        this.others = others;
    }

    public int getServicecompanychange() {
        return servicecompanychange;
    }

    public void setServicecompanychange(int servicecompanychange) {
        this.servicecompanychange = servicecompanychange;
    }

    public int getCharacteristicscompanychange() {
        return characteristicscompanychange;
    }

    public void setCharacteristicscompanychange(int characteristicscompanychange) {
        this.characteristicscompanychange = characteristicscompanychange;
    }

    public int getBuydatecompanychange() {
        return buydatecompanychange;
    }

    public void setBuydatecompanychange(int buydatecompanychange) {
        this.buydatecompanychange = buydatecompanychange;
    }

    public int getPayingmethodcompanychange() {
        return payingmethodcompanychange;
    }

    public void setPayingmethodcompanychange(int payingmethodcompanychange) {
        this.payingmethodcompanychange = payingmethodcompanychange;
    }

    public int getPricecompanychange() {
        return pricecompanychange;
    }

    public void setPricecompanychange(int pricecompanychange) {
        this.pricecompanychange = pricecompanychange;
    }

    public int getOtherscompanychange() {
        return otherscompanychange;
    }

    public void setOtherscompanychange(int otherscompanychange) {
        this.otherscompanychange = otherscompanychange;
    }

    public int getValidate_user() {
        return validate_user;
    }

    public void setValidate_user(int validate_user) {
        this.validate_user = validate_user;
    }

    public int getValidate_company() {
        return validate_company;
    }

    public void setValidate_company(int validate_company) {
        this.validate_company = validate_company;
    }

    @Override
    public int describeContents() {
        return this.hashCode();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        //Parcelable object
        dest.writeParcelable(date, flags);

        dest.writeString(service);
        dest.writeString(characteristics);
        dest.writeString(buydate);
        dest.writeString(payingmethod);
        dest.writeString(price);
        dest.writeString(others);
        dest.writeInt(servicecompanychange);
        dest.writeInt(characteristicscompanychange);
        dest.writeInt(buydatecompanychange);
        dest.writeInt(payingmethodcompanychange);
        dest.writeInt(pricecompanychange);
        dest.writeInt(otherscompanychange);
        dest.writeInt(validate_user);
        dest.writeInt(validate_company);

    }

If the CategoryDate class is one of yours, you can make it Parcelable as well. 如果CategoryDate类是您的类之一,则也可以将其设置为Parcelable Then in your CloseItPending class' writeToParcel() call you can call this.date.writeToParcel() and pass it the same Parcel object. 然后在CloseItPending类的writeToParcel()调用中,可以调用this.date.writeToParcel()并将其传递给同一Parcel对象。 This will cause the CategoryDate class to write its data into the same Parcel object which is being used by CloseItPending . 这将导致CategoryDate类将其数据写入由CloseItPending使用的同一Parcel对象中。

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

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