嵌套 Class 可打包

Nested Class Parcelable

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

当我创建嵌套 class 时出现错误(它需要包裹作为参数。)

代码:

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没有无参构造函数。要解决这个问题,只需添加一个无参数构造函数:

public Appointment() {}

"Why did this work before I implemented Parcelable?" 你可能会问。这是因为如果 class 没有构造函数,则会自动隐式地为您添加一个无参数构造函数。因此,在您添加带有 in 参数的构造函数后,原始的无参数构造函数将丢失。