扩展 Parcelable 以读取另一个 Parcelable

发布于 2024-12-01 14:25:36 字数 482 浏览 1 评论 0原文

我有一个扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2024-12-08 14:25:36

您可以将 null 作为 ClassLoader 传递以使用默认值。

Parcel.readTypedList()Parcel.writeTypedList() 似乎没问题。或者您可以使用 Parcel.readParcelableArray()Parcel.writeParcelableArray() 并将列表转换为数组。

You can pass null as ClassLoader to use the default one.

Parcel.readTypedList() and Parcel.writeTypedList() seem to be OK. Or you can use Parcel.readParcelableArray() and Parcel.writeParcelableArray() and convert the list to array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文