1

我已经在我的许多只有 String 成员变量的类中实现了 parcelable。我不确定如何为具有 ArrayList 的类执行此操作。我在接受源(Parcel)的构造函数中做什么?

public class Schedule {

private ArrayList<Event> allEvents;

 public Schedule(Parcel source) {

    }

public Schedule(){
    allEvents = new ArrayList<Event>();
}

//Getter
public ArrayList<Event> getAllEvents(){
    return allEvents;
}

public void addEvent(Event newEvent){
    allEvents.add(newEvent);
}

  public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeList(allEvents);
    }

        public static final Parcelable.Creator<Schedule> CREATOR = 
        new Parcelable.Creator<Schedule>() {

            @Override
            public Schedule createFromParcel(Parcel source) {
                return new Schedule(source);
            }

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

}

4

0 回答 0