24
public class Category implements Parcelable {

    private int mCategoryId;
    private List<Video> mCategoryVideos;

 public int getCategoryId() {
        return mCategoryId;
    }

    public void setCategoryId(int mCategoryId) {
        this.mCategoryId = mCategoryId;
    }

 public List<Video> getCategoryVideos() {
        return mCategoryVideos;
    }

    public void setCategoryVideos(List<Video> videoList) {
        mCategoryVideos = videoList;
    }

@Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(mCategoryId);
        parcel.writeTypedList(mCategoryVideos);
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Category createFromParcel(Parcel parcel) {
            final Category category = new Category();

            category.setCategoryId(parcel.readInt());
            category.setCategoryVideos(parcel.readTypedList()); */// **WHAT SHOULD I WRITE HERE***

            return category;
        }

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

}

我的代码我正在使用从 parcelable 实现的模型...谁能告诉他们我在这一行中写了什么我category.setCategoryVideos(parcel.readTypedList()) 找不到任何有用的帖子。

编辑:category.setCategoryVideos(parcel.readTypedList(mCategoryVideos,Video.CREATOR));在这里 mCategoryVideos 我无法解决错误。

4

3 回答 3

36

Parcelable 类有列表方法,你可以在这里查看它们:

readList(列出 outVal,ClassLoader 加载器)

writeList(列表值)

在您的情况下,它看起来像:

List<Object> myList = new ArrayList<>();

parcel.readList(myList,List.class.getClassLoader());
category.setCategoryVideos(myList);
于 2013-03-21T09:37:58.433 回答
6

从一个好的答案:

简单的步骤:

private List<MyParcelableClass> mList;

protected MyClassWithInnerList(Parcel in) {
    mList = in.readArrayList(MyParcelableClass.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeList(mList);
}
于 2017-06-20T11:28:03.497 回答
2
public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() {
            public Category createFromParcel(Parcel in) {
                return new Category(in);
            }

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

private Category(Parcel in) {
    String[] data = new String[1];
    in.readStringArray(data);
    mCategoryId = Integer.parseInt(data[0]);
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeStringArray(new String[]{
            mCategoryId
    });
}

然后在你的活动中。

public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelableArrayList("mCategoryVideos", (List<? extends Parcelable>) mCategoryVideos);
}

public void onRestoreInstanceState(Bundle inState) {
    super.onRestoreInstanceState(inState);
    if (inState != null) {
        mCategoryVideos = inState.getParcelableArrayList("mCategoryVideos");
        // Restore All Necessary Variables Here
    }
}
于 2013-03-21T09:37:43.560 回答