1

I have implemented parcelable in many of my classes that only have String member variables. I am not sure how to do it for a class that has an ArrayList. What do I do in the constructor that takes in the source (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];
            }
        };

}

user1154644
  • 4,491
  • 16
  • 59
  • 102
  • 1
    try [readArrayList(null)](http://developer.android.com/reference/android/os/Parcel.html#readArrayList%28java.lang.ClassLoader%29) – zapl Apr 29 '12 at 02:44
  • Check out the answer [here](http://stackoverflow.com/questions/10071502/how-to-read-write-array-of-parcelable-objects-android/10072382#10072382) to see if it helps. – yorkw Apr 29 '12 at 02:46
  • Try using http://www.parcelabler.com/ – EpicPandaForce Apr 01 '15 at 11:16

0 Answers0