Reputation: 41
Inside my Exercise class, I have a 2D ArrayList. I have managed to pass all of the fields from Activity to another using Parcelable. However, I cannot figure out how to pass the 2D ArrayList within my object using Parcelable.
public class Exercise implements Parcelable{
private String name, equipmentRequired;
private ArrayList musclesWorked;
private HashMap personalBest;
private boolean selected;
private float minAccelLevel, minAccelChanges;
private ArrayList<ArrayList<Double>> sets = new ArrayList<>();
...
private Exercise(Parcel in) {
name = in.readString();
equipmentRequired = in.readString();
musclesWorked = in.readArrayList(ArrayList.class.getClassLoader());
personalBest = in.readHashMap(HashMap.class.getClassLoader());
selected = in.readInt() != 0;
// sets = ???????
}
public static final Parcelable.Creator<Exercise> CREATOR = newParcelable.Creator<Exercise>() {
public Exercise createFromParcel(Parcel in) {
return new Exercise(in);
}
public Exercise[] newArray(int size) {
return new Exercise[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(equipmentRequired);
dest.writeList(musclesWorked);
dest.writeMap(personalBest);
dest.writeInt(selected ? 1 : 0);
// ?????
}
}
Upvotes: 3
Views: 183
Reputation: 41
Managed to solve the problem myself. Thanks.
...
private Exercise(Parcel in) {
...
sets = in.readArrayList(ArrayList.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
...
dest.writeList(sets);
}
}
Upvotes: 1
Reputation: 2210
According to Google Documentation and this other question, I would suggest going with
private Exercise(Parcel in) {
name = in.readString();
equipmentRequired = in.readString();
musclesWorked = in.readArrayList(ArrayList.class.getClassLoader());
personalBest = in.readHashMap(HashMap.class.getClassLoader());
selected = in.readInt() != 0;
sets = in.readArrayList(null);
}
This would be because, as the doc states:
The given class loader will be used to load any enclosed Parcelables.
Upvotes: 1