Android使用Intent发送Parcelable对象列表,其中包含另一个Parcelable对象的列表

时间:2016-09-15 03:12:02

标签: android android-intent

在我的Android应用中,我想使用IntentSingleGroup的列表传递给另一个Activity,其中每个SingleGroup对象都包含{{1}的列表},这里是SingleImageSingleImage的类:

SingleGroup

这是SingleGroup类:

public class SingleImage implements Parcelable{

public  String name;
public  int drawableId;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getDrawableId() {
    return drawableId;
}

public void setDrawableId(int drawableId) {
    this.drawableId = drawableId;
}

public SingleImage(String name, int drawableId)
{
    this.name = name;
    this.drawableId = drawableId;
}

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

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

private SingleImage(Parcel in)
{
    name = in.readString();
    drawableId = in.readInt();
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeInt(drawableId);
}
}

在致电public class SingleGroup implements Parcelable{ private static final String TAG = "SINGLEGROUP"; private ArrayList<SingleImage> images = new ArrayList<SingleImage>(0); private String myTitle; private String groupDesc; public SingleGroup(String mTitle, String desc, ArrayList<SingleImage> imagesList) { this.myTitle = mTitle; this.groupDesc = desc; this.images = imagesList; } public String getGroupDesc() { return groupDesc; } public String getMyTitle() { return myTitle; } public void setMyTitle(String myTitle) { this.myTitle = myTitle; } public ArrayList<SingleImage> getImages() { return images; } public void setImages(ArrayList<SingleImage> images) { this.images = images; } public static final Parcelable.Creator<SingleGroup> CREATOR = new Parcelable.Creator<SingleGroup>() { public SingleGroup createFromParcel(Parcel in) { return new SingleGroup(in); } public SingleGroup[] newArray(int size) { return new SingleGroup[size]; } }; private SingleGroup(Parcel in) { myTitle = in.readString(); groupDesc = in.readString(); //problem maybe here in.readTypedList(images,SingleImage.CREATOR); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(myTitle); dest.writeString(groupDesc); //problem maybe here dest.writeTypedList(images); } @Override public String toString() { String result = "myTitle "+myTitle+" desc "+ groupDesc + " imagesize "+images.size(); return result; } } ArrayList<SingleImage>时,它可以正常工作, 当我打电话通过Intent通过ArrayLlist<SingleGroup>时,我收到了错误:

  

包裹android.os.Parcel@3c4c6743:解组未知类型代码   3211379在偏移224

有什么想法吗?提前致谢

----------------更新----------------
我已将Intent更改为in.readTypedList(images, null);但仍无效。同样的解组错误

----------------解决问题--------------
感谢凯塔。真正的问题实际上没有在我的代码中列出。当我通过可分配对象时,我还应该包括一些我错过的其他属性 强烈推荐AS插件Android Parcelable code generator

3 个答案:

答案 0 :(得分:2)

您必须将SingleImage.CREATOR传递给方法readTypedList而不是“null”值

private SingleGroup(Parcel in)
{
    myTitle = in.readString();
    groupDesc = in.readString();

     //problem maybe here
    in.readTypedList(images, SingleImage.CREATOR);
}

答案 1 :(得分:0)

试试这个。这就是我一直以来所做的工作:

private SingleGroup(Parcel in)
{
    myTitle = in.readString();
    groupDesc = in.readString();

     //problem maybe here
    in.createTypedArrayList(SingleImage.CREATOR);
}

答案 2 :(得分:0)

ArrayList 序列化。所以试试这个

public class SingleImage implements Serializable{ .... }

请参阅:ArrayList Android Documentation