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

时间:2014-06-12 09:52:06

标签: android class object parcelable

我想创建一个parcelable类,但其中一个变量是另一个对象,所以我不知道如何管理它。

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;

    ...

如您所见,名为date且类型的第二个变量是CategoryDate。

接下来的方法必须如何?

public int describeContents()

public void writeToParcel(Parcel dest, int flags)

感谢您的帮助。

可能性:

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

    }

1 个答案:

答案 0 :(得分:1)

如果CategoryDate课程属于您的课程,您也可以Parcelable。然后在CloseItPending课程中#39; writeToParcel()来电可以致电this.date.writeToParcel()并传递相同的Parcel对象。这将导致CategoryDate类将其数据写入Parcel正在使用的同一CloseItPending对象。