如何编写List<>进入包裹 [英] How to write List<> into parcel
问题描述
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())
我找不到任何有用的帖子.
Im my code I am using model which implements from parcelable...Could anyone tell em what shout I write in this line category.setCategoryVideos(parcel.readTypedList())
I couldn't find any helpful post.
category.setCategoryVideos(parcel.readTypedList(mCategoryVideos,Video.CREATOR));
在这里,我无法解决错误的mCategoryVideos.
category.setCategoryVideos(parcel.readTypedList(mCategoryVideos,Video.CREATOR));
in here mCategoryVideos I have cannot resolve error.
推荐答案
Parcelable类有一些列表方法,您可以在这里查看它们:
There are list methods for Parcelable class, you can take a look at them here:
readList(列出outVal,ClassLoader加载程序)
在您的情况下,它看起来像:
In your case it would look like:
List<Object> myList = new ArrayList<>();
parcel.readList(myList,List.class.getClassLoader());
category.setCategoryVideos(myList);
这篇关于如何编写List<>进入包裹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!