There is an interface DrawingElement
(extending Parcelable
) with multiple implementations. There is a field List<DrawingElement> elements
in another class, which implements Parcelable
. So the List should be written to the Parcel, but the method for reading Parcelable ArrayList requires a concrete class for referencing the concrete implementation of a Creator and this is not working: in.createTypedArrayList(DrawingElement.CREATOR)
.
public interface DrawingElement extends Parcelable {
}
public class FirstDrawingElementImpl implements DrawingElement {
@Override
public void writeToParcel(Parcel dest, int flags) {
// done easily.
}
}
public class SecondDrawingElementImpl implements DrawingElement {
@Override
public void writeToParcel(Parcel dest, int flags) {
// done easily.
}
}
public class DrawingElementsContainer implements Parcelable {
private List<DrawingElement> elements;
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO:
}
}
Any suggestions?
UPDATE:
If I had to use the @pskink suggestion, I had to do weird casting like:
protected Drawing(Parcel in) {
DrawingElement[] parcelables = (DrawingElement[]) in.readParcelableArray(DrawingElement.class.getClassLoader());
elements = new ArrayList<>(Arrays.asList(parcelables));
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelableArray(elements.toArray(new Parcelable[elements.size()]), flags);
}
dest.writeList(elements)
is the obvious decision, but I got confused by passing an interface classloader.
Use below code to define your class. parcel.readArrayList
method is used to read the list from the parcel and parcel.wrtieList
method is used to write list to the parcel. Refer this answer.
public class DrawingElementsContainer implements Parcelable {
private ArrayList<DrawingElement> elements;
public DrawingElementsContainer(Parcel in) {
elements=in.readArrayList(DrawingElement.class.getClassLoader());
}
public ArrayList<DrawingElement> getElements() {
return elements;
}
public void setElements(ArrayList<DrawingElement> elements) {
this.elements = elements;
}
@Override
public int describeContents() {
return 0;
}
public DrawingElementsContainer()
{
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(elements);
}
public static final Parcelable.Creator<DrawingElementsContainer> CREATOR
= new Parcelable.Creator<DrawingElementsContainer>() {
public DrawingElementsContainer createFromParcel(Parcel in) {
return new DrawingElementsContainer(in);
}
public DrawingElementsContainer[] newArray(int size) {
return new DrawingElementsContainer[size];
}
};
}
Hope it might help you