实现 Parcelable 接口时如何读/写布尔值?
我正在尝试创建一个 ArrayList ,以便将自定义对象列表传递给活动。我开始编写一个 myObjectList
类,它扩展 ArrayList
并实现 Parcelable
。
MyObject
的某些属性是 boolean
,但 Parcel
没有任何方法 read/writeBoolean
。
处理这个问题的最佳方法是什么?
I'm trying to make an ArrayList
Parcelable
in order to pass to an activity a list of custom object. I start writing a myObjectList
class which extends ArrayList<myObject>
and implement Parcelable
.
Some attributes of MyObject
are boolean
but Parcel
don't have any method read/writeBoolean
.
What is the best way to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您可以使用掩码和移位将布尔值打包到一个字节中。这将是最有效的方法,也可能是他们希望你做的。
You could pack your boolean values into a byte using masking and shifting. That would be the most efficient way to do it and is probably what they would expect you to do.
这里很难确定真正的问题。我想这就是实现
Parcelable
接口时如何处理布尔值。您必须将该值存储为字符串或字节。如果您选择字符串,则必须使用
String
类的静态方法valueOf()
来解析布尔值。它不如将其保存在一个字节中那么有效。如果您选择一个字节,则必须自己实现转换逻辑。
解组
Parcel
对象时,您必须注意到原始类型的转换。It is hard to identify the real question here. I guess it is how to deal with booleans when implementing the
Parcelable
interface.You will have to either store the value as a string or as a byte. If you go for a string then you'll have to use the static method of the
String
class calledvalueOf()
to parse the boolean value. It isn't as effective as saving it in a byte tough.If you go for a byte you'll have to implement a conversion logic yourself.
When unmarshalling the
Parcel
object you have to take care of the conversion to the original type.这个问题已经被其他人完美回答了,如果你想自己做的话。
如果您希望封装或隐藏大部分低级打包代码,您可以考虑使用 我之前编写的一些代码用于简化对 Parcelable 的处理。
写入 Parcel 非常简单:
例如,其中 color 是一个枚举,isDriving 是一个布尔值。
从包裹中读取数据也并不困难:
只需查看我添加到项目中的“ParceldroidExample”即可。
最后,它还使 CREATOR 初始值设定项保持简短:
This question has already been answered perfectly by other people, if you want to do it on your own.
If you prefer to encapsulate or hide away most of the low-level parceling code, you might consider using some of the code I wrote some time ago for simplifying handling of parcelables.
Writing to a parcel is as easy as:
where color is an enum and isDriving is a boolean, for example.
Reading from a parcel is also not much harder:
Just take a look at the "ParceldroidExample" I added to the project.
Finally, it also keeps the CREATOR initializer short:
Android (AOSP) 源代码中有很多示例。例如,
PackageInfo
类有一个布尔成员requiredForAllUsers
,它的序列化如下:There are many examples in the Android (AOSP) sources. For example,
PackageInfo
class has a boolean memberrequiredForAllUsers
and it is serialized as follows:writeToParcel:
dest.writeBoolean(booleanFlag);
readFromParcel:
booleanFlag = in.readBoolean()
writeToParcel:
dest.writeByte((byte) (booleanFlag ? 1 : 0));
readFromParcel:
booleanFlag = in.readByte() != 0;
writeToParcel:
dest.writeBoolean(booleanFlag);
readFromParcel:
booleanFlag = in.readBoolean()
writeToParcel:
dest.writeByte((byte) (booleanFlag ? 1 : 0));
readFromParcel:
booleanFlag = in.readByte() != 0;
从 api 29 开始,您现在可以在
Parcel
类中使用readBoolean()
。请参阅 https://developer.android.com/reference/android/os /Parcel#readBoolean() 了解更多
Since api 29, you can now use the
readBoolean()
in theParcel
class.see https://developer.android.com/reference/android/os/Parcel#readBoolean() for more
这是我的做法...
writeToParcel:
readFromParcel:
Here's how I'd do it...
writeToParcel:
readFromParcel:
您还可以使用 writeValue< /a> 方法。在我看来,这是最直接的解决方案。
之后,您可以通过简单地转换为
Boolean
轻松检索它:在幕后,Android 框架会将其作为整数处理:
You could also make use of the writeValue method. In my opinion that's the most straightforward solution.
Afterwards you can easily retrieve it with a simple cast to
Boolean
:Under the hood the Android Framework will handle it as an integer:
您像这样声明
write
read
boolean 类型需要转换为 Parcel 支持的类型,因此我们可以将其转换为 int。
you declare like this
write
read
boolean type needs to be converted to something that Parcel supports and so we can convert it to int.
AndroidStudio(使用 2.3 atm),在您的类上实现 Parcelable 后,您只需将鼠标指针悬停在您的类名称上,它就会要求您添加 Parcelable 实现:
来自四个字段,它生成以下内容:
AndroidStudio (using 2.3 atm), after implementing Parcelable on your class, you can simply hold your mouse pointer over your class name and it asks you to add the parcelable implementation:
From the four fields, it generates the following:
我通常将它们放在一个数组中并调用 writeBooleanArray 和 readBooleanArray
如果您需要打包单个布尔值,您可以这样做:
I normally have them in an array and call
writeBooleanArray
andreadBooleanArray
If it's a single boolean you need to pack, you could do this:
在 Kotlin 中实现简短且可空的支持:
向 Parcel 添加方法
并使用它:
Short and simple implementation in Kotlin, with nullable support:
Add methods to Parcel
And use it:
只需转到文件->设置->插件->浏览存储库并搜索parcelable。参见图片
它会自动创建 Parcelable。
还有一个网站也可以这样做。 http://www.parcelabler.com/
Simply go to File->Settings->Plugins->Browse Repository and search for parcelable .See image
It will automatically create Parcelable.
And there is a webiste also for doing this. http://www.parcelabler.com/