繁体   English   中英

将列表写入Parcelable?

[英]Writing a List to Parcelable?

我有一个自定义对象,具有private List<AnotherCustomObject> mList; 需要注意的是AnotherCustomObject实现Parcelable

但是,如果我想要写mListdestwriteToParcel()然后读回来后,这才是正确的方法:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM