Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
700 views
in Technique[技术] by (71.8m points)

java - Android Parcelable - read/write data to Parcel using generic data type

How can i implement to write my Set < ArrayList < ? > > to my Parcel using generic data type ?

Here is my code..

        dest.writeList(getArrTRA());
        dest.writeList(getArrTSAC());
        dest.write???(getArrListSet()); //how can i write my Set<ArrayList<?


public Set<ArrayList<?>> getArrListSet() {
        return arrSetOfPaymentMode;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
public class ImageContainer implements Serializable, Parcelable  {
/**
 * 
 */
private static final long serialVersionUID = 1L;

public ImageContainer() {
    // TODO Auto-generated constructor stub
}
public ImageContainer(Parcel in) {
    // TODO Auto-generated constructor stub
    readFromParcel(in);
}


/**
 * custom images in this article
 */
@SerializedName("Image")
ArrayList<ImageCls> alCustomImages=new ArrayList<ImageCls>();

public ArrayList<ImageCls> getAlCustomImages() {
    return alCustomImages;
}
public void setAlCustomImages(ArrayList<ImageCls> alCustomImages) {
    this.alCustomImages = alCustomImages;
}
@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    dest.writeList(alCustomImages);
}
@SuppressWarnings("unchecked")
private void readFromParcel(Parcel in) {
    // TODO Auto-generated method stub
    this.alCustomImages = in.readArrayList(ImageCls.class.getClassLoader());
}

@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public ImageContainer createFromParcel(Parcel in) {
        return new ImageContainer(in);
    }

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

your class should implement Parcelable. An example is in above code.

Your issue is you are trying to read/write generic data type to the Parcel. But it is not possible to read/write generic data type in parcel. Read more how to use Parcelable here and Parcel here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...