[英]Writing a List to Parcelable?
我有一个自定义对象,具有private List<AnotherCustomObject> mList;
。 需要注意的是AnotherCustomObject
实现Parcelable
。
但是,如果我想要写mList
到dest
在writeToParcel()
然后读回来后,这才是正确的方法:
dest.writeList(mList);
然后在read方法中
in.readList(mList, ClassLoader.getSystemClassLoader());
正确?
请尝试使用必要的类加载器。
in.readList(mList, AnotherCustomObject.getClass().getClassLoader());
可以使用readTypedList(List<T> list, Creator<T> c)
方法读取List<AnotherCustomObject> mList
readTypedList(List<T> list, Creator<T> c)
并使用writeTypedList(List<T> val)
方法写入。
在writeToParcel
方法中,您将编写以下内容:
dest.writeTypedList(mList);
然后在从Parcel
对象创建实例的构造函数中,您可以放置:
in.readTypedList(mList, AnotherCustomObject.CREATOR);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.