2
votes

I am new to android, and making a doctor's appointment managing android app using ThreeTenABP's (so it's compatible with more devices) LocalDate and LocalTime, which I need to make parcelable.

I have the parcelable complex class Appointment, which has instances of LocalDate and LocalTime as attributes; classes I don't think are parcelable by default.

I don't want to change the logic to work with different classes, or primitives even; since these classes are used widely throughout the app. Naturally, these attributes are not put in the Appointment(Parcel in) method automatically, and I don't know how to include them, or if it's even possible.

Performance is very important, so I'm not considering Serializable as an option either.

This is the Appointment class (also, I made sure to make all other custom objects parcelable):

public class Appointment implements Parcelable{

    private Patient patient;
    private LocalDate date;
    private LocalTime time;
    private Doctor doctor;
    private Prescription prescription;

    public Appointment(Patient patient, LocalDate date, LocalTime time, Doctor doctor, Prescription prescription) {

        this.patient = patient
        this.date = date;
        this.time = time;
        this.doctor = doctor;
        this.prescription = prescription;
    }

    protected Appointment(Parcel in) {
        patient = in.readParcelable(Patient.class.getClassLoader());
        doctor = in.readParcelable(Doctor.class.getClassLoader());
        prescription = in.readParcelable(Prescription.class.getClassLoader());
    }

    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];
        }
    };

    //Class methods

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(patient, flags);
        dest.writeParcelable(doctor, flags);
        dest.writeParcelable(prescription, flags);
    }
}

I already tried adding date and time to Appointment(Parcel in) and writeToParcel() just like the other attributes, but it says parameters are the wrong type:

Wrong 1st argument type. Found: 'org.threeten.bp.LocalDate', required: 'android.os.Parcelable'

I'm not getting any error messages if I leave it withput date and time, but the app crashes when it gets to the intent.putExtra() method to pass the object to the corresponding activity.

Please, help

1
"Performance is very important, so I'm not considering Serializable as an option either" Did you actually measure whether it has a significant impact in your code? Writing/reading as Serializable would be the easy way here.laalto
I'm gonna be honest, I hadn't tried Serializable and it works. Thank you! I would like to change it to parcelable in the future if possible, though.Marianne Rojas
You only need Serializable with these fields in Parcel, not all over the placelaalto

1 Answers

3
votes
@Override
protected Appointment(Parcel in) {
    // Read objects
    date = (LocalDate) in.readSerializable();
    time = (LocalTime) in.readSerializable();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    // Write objects 
    dest.writeSerializable(date);
    dest.writeSerializable(time);
}

This post here explains the performance details so well. I don't have to repeat them here.

A more performant approach would be converting your date objects to long and while writing to a parcel and converting them back to the relevant date objects when reading from the parcel.