1

To pass an arrayList of objects to a fragment, I have to make the list objects parcelable.

public mObjectClass implements Parcelable {

    // Some code

}

The problem is that one of the attributes in my list objects is another object-based arrayList.

public mObjectClass implements Parcelable {

        // Some code

        private ArrayList<someOtherObject> anotherArrayList;

    }

How can I make mObjectClass parcelable?

the_prole
  • 8,275
  • 16
  • 78
  • 163

2 Answers2

1

someOtherObject has to implement Parcelable (not extend has in your question) too. Then you can call parcel.writeTypedList(anotherArrayList); to write it and parcel.readTypedList(yourList, someOtherObject.CREATOR) to read it back. You can read more here

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Is [this](http://stackoverflow.com/questions/14178736/how-to-make-a-class-with-nested-objects-parcelable) the same question as mine? – the_prole Nov 12 '15 at 20:48
  • Thanks. Let me figure it out, and I will check mark you when I fully understand your answer. – the_prole Nov 12 '15 at 20:53
0

This solution is heavily influenced by some Stackoverflow posts, including this.

Essentially make both Classes parcelable and make use of someOtherClasses Parcelable.Creator.

mObjectClass:

public class mObjectClass implements Parcelable {

    private ArrayList<someOtherObject> anotherArrayList;

    //add getter + setter...

    public mObjectClass() {
        anotherArrayList = new ArrayList<someOtherObject>();
    }

    public mObjectClass(Parcel in) {

        anotherArrayList = new ArrayList<someOtherObject>();
        in.readTypedList(anotherArrayList, someOtherObject.CREATOR);
    }

    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel outParcel, int flags) {

        outParcel.writeTypedList(anotherArrayList);
    }

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

        @Override
        public mObjectClass createFromParcel(Parcel in) {
            return new mObjectClass(in);
        }

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

someOtherObject:

public class someOtherObject implements Parcelable {
    String someString;

    //add getter + setter...

    public void writeToParcel(Parcel out, int flags) {

        out.writeString(someString);

    }

    public static final Parcelable.Creator<someOtherObject> CREATOR = new Parcelable.Creator<someOtherObject>() {
        public someOtherObject createFromParcel(Parcel in) {
            return new someOtherObject(in);
        }

        public someOtherObject[] newArray(int size) {
            return new someOtherObject[size];
        }
    };

    public int describeContents() {
        return 0;
    }

    public someOtherObject(Parcel in) {
        someString = in.readString();

    }

    public someOtherObject() {
    }

    public someOtherObject(String someString) {
        this.someString = someString;
    }
}

Tada, you are now able to add mObjectClass as extra to your intents after initialising it and using a setter to set the Arraylist with other someOtherObjects.

Community
  • 1
  • 1
tritop
  • 1,655
  • 2
  • 18
  • 30