首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在parcel中写入List<>

如何在parcel中写入List<>
EN

Stack Overflow用户
提问于 2013-03-21 16:57:13
回答 3查看 25.7K关注 0票数 24
代码语言:javascript
复制
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...Could实现的模型,有人告诉他们我在这一行写了什么吗?category.setCategoryVideos(parcel.readTypedList()),我找不到任何有用的帖子。

编辑:category.setCategoryVideos(parcel.readTypedList(mCategoryVideos,Video.CREATOR)); in here mCategoryVideos I have cannot resolve error。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-21 17:37:58

Parcelable类有list方法,你可以在这里看看:

readList (List outVal, ClassLoader loader)

writeList (List val)

在您的示例中,它将如下所示:

代码语言:javascript
复制
List<Object> myList = new ArrayList<>();

parcel.readList(myList,List.class.getClassLoader());
category.setCategoryVideos(myList);
票数 36
EN

Stack Overflow用户

发布于 2017-06-20 19:28:03

From a good answer:

简单的步骤:

代码语言:javascript
复制
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);
}
票数 6
EN

Stack Overflow用户

发布于 2013-03-21 17:37:44

代码语言:javascript
复制
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
    });
}

然后在你的活动中。

代码语言:javascript
复制
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
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15543033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档