Reputation: 180
I have browsed the question on Stack and I found no question facing the particular problem. I have a class with two arraylist of custom objects. I am not sure how to implement writeToParcel for the parent class now. One option I found were readTypedList constructor, but I cant implement it twice. There is no readTypedListArray constructor that I can create an array ans send it.
Just a reminder the object are ArrayList and there are two such objects in the parent class. The github link for reference is here.
Upvotes: 0
Views: 1159
Reputation: 1
In your MovieObject.java
, you can declare them as List<YOUR_CLASS>
instead of ArrayList<YOUR_CLASS>
.
In my code:
public class CombinedClass implements Parcelable {
private List<Class1> class1List;
private List<Class2> class2List;
//Constructor, getter, setter, methods.
}
Both Class1 and Class2 implements Parcelable too. I'm using "auto-generated" Parcelable implementation provided by Android Studio. Here is the Parcelable implementation:
protected CombinedClass(Parcel in) {
class1List = in.createTypedArrayList(Class1.CREATOR);
class2List = in.createTypedArrayList(Class2.CREATOR);
}
public static final Creator<CombinedClass> CREATOR = new Creator<CombinedClass>() {
@Override
public CombinedClass createFromParcel(Parcel in) {
return new CombinedClass(in);
}
@Override
public CombinedClass[] newArray(int size) {
return new CombinedClass[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeTypedList(class1List);
parcel.writeTypedList(class2List);
}
Upvotes: 0
Reputation: 348
You can try below workaround in MovieObject.java
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] {
this.OrignalTitle,
this.MoviePosterImageThumbnailUrl,
this.PlotSynopsis,
this.UserRating,
this.ReleaseDate
});
dest.writeLongArray(new long[] {
this._id,
this.ApiId
});
ArrayList<ArrayList> temp = new List<>();
temp.add(ReviewObjs);
temp.add(TrailerVideoObjs);
dest.writeTypedList(temp);
}
Upvotes: 0
Reputation: 2727
You can add customArray using following way :
In MovieObject
class:
@Expose
private ArrayList< ReviewObject > ReviewObjs = new ArrayList< ReviewObject >();
private ArrayList< TrailerVideoObject > TrailerVideoObjs = new ArrayList< TrailerVideoObject >();
writeToParcel() :
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(ReviewObjs);
dest.writeTypedList(TrailerVideoObjs);
}
and readFromParcel() :
private void readFromParcel(Parcel in) {
ReviewObjs = new ArrayList<ReviewObject>();
in.readTypedList(ReviewObjs, ReviewObject.CREATOR);
TrailerVideoObjs = new ArrayList<TrailerVideoObject>();
in.readTypedList(TrailerVideoObjs, TrailerVideoObject.CREATOR);
}
And add following lines in CustomClasses :
for ReviewObject
:
public class ReviewObject implements Parcelable {
public static final Creator<ReviewObject> CREATOR = new Creator<ReviewObject>() {
public ReviewObject createFromParcel(Parcel in) {
return new ReviewObject(in);
}
public ReviewObject[] newArray(int size) {
return new ReviewObject[size];
}
};
// add other objects...
}
for TrailerVideoObject
:
public class TrailerVideoObject implements Parcelable {
public static final Creator<TrailerVideoObject> CREATOR = new Creator<TrailerVideoObject() {
public TrailerVideoObject createFromParcel(Parcel in) {
return new TrailerVideoObject(in);
}
public TrailerVideoObject[] newArray(int size) {
return new TrailerVideoObject[size];
}
};
// add other objects...
}
Upvotes: 4