扩展 Parcelable 以读取另一个 Parcelable
我有一个扩展 Parcelable 的类,我们将其称为 A 类。
我有另一个扩展 Parcelable 的类,我们将其称为 B 类
。A 的成员变量之一是 B 类对象的 ArrayList。
我正在尝试编写 Parcelable 重写,但不知道如何保存和读取 B 类的 ArrayList。
有一些方法可以读取和写入 ArrayList,但它们需要一个 ClassLoader 参数,而我不熟悉它。我还可以将 ArrayList 复制到数组中,并使用方法来读取和写入 Parcelable 数组,但这也需要 ClassLoader 参数。
更新:
它看起来像
in.readTypedList(mList, ClassB.CREATOR);
我
out.writeTypedList(mList);
正在寻找的东西吗?
I have a class that extends Parcelable, we'll call it Class A.
I have another class that extends Parcelable, we'll call it Class B.
One of the member variables of A is an ArrayList of Class B objects.
I'm trying to write the Parcelable overrides, but can't figure out how to save and read the ArrayList of Class B.
There are methods to read and write ArrayLists, but they want a parameter which is a ClassLoader and I am unfamiliar with it. I could also copy the ArrayList into an array and user the methods to read and write a Parcelable array, but this also requires a ClassLoader parameter.
Update:
It looks like
in.readTypedList(mList, ClassB.CREATOR);
and
out.writeTypedList(mList);
are what I'm looking for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
null
作为ClassLoader
传递以使用默认值。Parcel.readTypedList()
和Parcel.writeTypedList()
似乎没问题。或者您可以使用Parcel.readParcelableArray()
和Parcel.writeParcelableArray()
并将列表转换为数组。You can pass
null
asClassLoader
to use the default one.Parcel.readTypedList()
andParcel.writeTypedList()
seem to be OK. Or you can useParcel.readParcelableArray()
andParcel.writeParcelableArray()
and convert the list to array.