[英]Writing a List to Parcelable?
I have a custom object that has private List<AnotherCustomObject> mList;
我有一个自定义对象,具有private List<AnotherCustomObject> mList;
. 。 Note that AnotherCustomObject
implements Parcelable
. 需要注意的是AnotherCustomObject
实现Parcelable
。
However if I want to write mList
to dest
in writeToParcel()
and then read it back later, is this the correct way: 但是,如果我想要写mList
到dest
在writeToParcel()
然后读回来后,这才是正确的方法:
dest.writeList(mList);
and then in the read method 然后在read方法中
in.readList(mList, ClassLoader.getSystemClassLoader());
Correct? 正确?
请尝试使用必要的类加载器。
in.readList(mList, AnotherCustomObject.getClass().getClassLoader());
Your List<AnotherCustomObject> mList
can be read using the readTypedList(List<T> list, Creator<T> c)
method and written to using the writeTypedList(List<T> val)
method. 可以使用readTypedList(List<T> list, Creator<T> c)
方法读取List<AnotherCustomObject> mList
readTypedList(List<T> list, Creator<T> c)
并使用writeTypedList(List<T> val)
方法写入。
In your writeToParcel
method you would write the following: 在writeToParcel
方法中,您将编写以下内容:
dest.writeTypedList(mList);
And then in the constructor which creates an instance from a Parcel
object you can put: 然后在从Parcel
对象创建实例的构造函数中,您可以放置:
in.readTypedList(mList, AnotherCustomObject.CREATOR);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.