嵌套类可拆分 [英] Nested Class Parcelable

查看:79
本文介绍了嵌套类可拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何制作嵌套类Parcelable.

I need to know how to make a nested class Parcelable.

当我创建一个嵌套类时,我得到一个错误(它需要包裹作为参数.)

When I create a nested class I get an error (it requires the parcel as a param.)

代码:

public class BookingDetailsItem implements Parcelable {
    private ServiceProviderItem serviceProvider;

    public Appointment appointmentDetails;
    private String notes;

    public BookingDetailsItem(ServiceProviderItem serviceProvider)
    {
        this.serviceProvider = serviceProvider;
        appointmentDetails = new Appointment();  // here it requires the parcel as required parameter
    }

    protected BookingDetailsItem(Parcel in) {
        serviceProvider = in.readParcelable(ServiceProviderItem.class.getClassLoader());
        appointmentDetails = in.readParcelable(Appointment.class.getClassLoader());
        notes = in.readString();
    }

    public static final Creator<BookingDetailsItem> CREATOR = new Creator<BookingDetailsItem>() {
        @Override
        public BookingDetailsItem createFromParcel(Parcel in) {
            return new BookingDetailsItem(in);
        }

        @Override
        public BookingDetailsItem[] newArray(int size) {
            return new BookingDetailsItem[size];
        }
    };

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(serviceProvider, flags);
        dest.writeParcelable(appointmentDetails, flags);
        dest.writeString(notes);
    }

    public static class Appointment implements Parcelable {
        private boolean isPickup;
        private Date servicingDate; // servicing date and time. In case of pickup, it will  be pick up date and time.

        private String additionalRequirements;

        protected Appointment(Parcel in) {
            isPickup = in.readByte() != 0;
            additionalRequirements = in.readString();
        }

        public static final Creator<Appointment> CREATOR = new Creator<Appointment>() {
            @Override
            public Appointment createFromParcel(Parcel in) {
                return new Appointment(in);
            }

            @Override
            public Appointment[] newArray(int size) {
                return new Appointment[size];
            }
        };

        public boolean isPickup() {
            return isPickup;
        }

        public void setPickup(boolean pickup) {
            isPickup = pickup;
        }

        public Date getServicingDate() {
            return servicingDate;
        }

        public void setServicingDate(Date servicingDate) {
            this.servicingDate = servicingDate;
        }

        public String getAdditionalRequirements() {
            return additionalRequirements;
        }

        public void setAdditionalRequirements(String additionalRequirements) {
            this.additionalRequirements = additionalRequirements;
        }

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

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeByte((byte) (isPickup ? 1 : 0));
            dest.writeString(additionalRequirements);
        }
    }
}

推荐答案

出现错误是因为Appointment没有无参数的构造函数.要解决此问题,只需添加一个无参数的构造函数:

The error appears because Appointment does not have a parameterless constructor. To fix this, just add a parameterless constructor:

public Appointment() {}

为什么要在实施Parcelable之前进行这项工作?"你可能会问.这是因为如果类没有构造函数,则会为您自动隐式添加无参数构造函数.因此,在使用in参数添加构造函数之后,原始的无参数构造函数就会丢失.

"Why did this work before I implemented Parcelable?" you might ask. This is because if a class has no constructors, a parameterless constructor is automatically and implicitly added for you. So after you added your constructor with the in parameter, the original parameterless constructor is lost.

这篇关于嵌套类可拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆