Víctor Martín
Víctor Martín

Reputation: 3450

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.

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

    }

Upvotes: 0

Views: 723

Answers (1)

Larry Schiefer
Larry Schiefer

Reputation: 15775

If the CategoryDate class is one of yours, you can make it Parcelable as well. Then in your CloseItPending class' writeToParcel() call you can call this.date.writeToParcel() and pass it the same Parcel object. This will cause the CategoryDate class to write its data into the same Parcel object which is being used by CloseItPending.

Upvotes: 1

Related Questions