将列表写入 Parcelable?
Writing a List to Parcelable?
我有一个自定义对象 private List<AnotherCustomObject> mList;
。请注意 AnotherCustomObject
实现了 Parcelable
.
但是,如果我想在writeToParcel()
中将mList
写入dest
,然后稍后再读回来,这是正确的方法吗:
dest.writeList(mList);
然后在读取方法中
in.readList(mList, ClassLoader.getSystemClassLoader());
正确吗?
请尝试使用必要的类加载器。
in.readList(mList, AnotherCustomObject.getClass().getClassLoader());
您的 List<AnotherCustomObject> mList
可以使用 readTypedList(List<T> list, Creator<T> c)
方法读取并使用 writeTypedList(List<T> val)
方法写入。
在您的 writeToParcel
方法中,您将编写以下内容:
dest.writeTypedList(mList);
然后在从 Parcel
对象创建实例的构造函数中,您可以输入:
in.readTypedList(mList, AnotherCustomObject.CREATOR);
我有一个自定义对象 private List<AnotherCustomObject> mList;
。请注意 AnotherCustomObject
实现了 Parcelable
.
但是,如果我想在writeToParcel()
中将mList
写入dest
,然后稍后再读回来,这是正确的方法吗:
dest.writeList(mList);
然后在读取方法中
in.readList(mList, ClassLoader.getSystemClassLoader());
正确吗?
请尝试使用必要的类加载器。
in.readList(mList, AnotherCustomObject.getClass().getClassLoader());
您的 List<AnotherCustomObject> mList
可以使用 readTypedList(List<T> list, Creator<T> c)
方法读取并使用 writeTypedList(List<T> val)
方法写入。
在您的 writeToParcel
方法中,您将编写以下内容:
dest.writeTypedList(mList);
然后在从 Parcel
对象创建实例的构造函数中,您可以输入:
in.readTypedList(mList, AnotherCustomObject.CREATOR);