如何使用 ArrayList正确实现 Parcelable?
我在使我的类Parcelable
时遇到问题。问题是,我试图向包裹写入类中的一个成员,该成员是一个 ArrayListArrayList
是可序列化
,列表中的对象 (ZigBeeDev
) 是可解析
。
这是相关代码:
package com.gnychis.coexisyst;
import java.util.ArrayList;
import java.util.Iterator;
import android.os.Parcel;
import android.os.Parcelable;
public class ZigBeeNetwork implements Parcelable {
public String _mac; // the source address (of the coordinator?)
public String _pan; // the network address
public int _band; // the channel
ArrayList<Integer> _lqis; // link quality indicators (to all devices?)
ArrayList<ZigBeeDev> _devices; // the devices in the network
public void writeToParcel(Parcel out, int flags) {
out.writeString(_mac);
out.writeString(_pan);
out.writeInt(_band);
out.writeSerializable(_lqis);
out.writeParcelable(_devices, 0); // help here
}
private ZigBeeNetwork(Parcel in) {
_mac = in.readString();
_pan = in.readString();
_band = in.readInt();
_lqis = (ArrayList<Integer>) in.readSerializable();
_devices = in.readParcelable(ZigBeeDev.class.getClassLoader()); // help here
}
public int describeContents() {
return this.hashCode();
}
public static final Parcelable.Creator<ZigBeeNetwork> CREATOR =
new Parcelable.Creator<ZigBeeNetwork>() {
public ZigBeeNetwork createFromParcel(Parcel in) {
return new ZigBeeNetwork(in);
}
public ZigBeeNetwork[] newArray(int size) {
return new ZigBeeNetwork[size];
}
};
//...
}
我标记了两个点“//help here”以了解如何正确写入包裹以及如何重建它。如果 ZigBeeDev
是 Parcelable
(经过正确测试),我该如何正确执行此操作?
I am having trouble making my class Parcelable
. The trouble is, I am trying to write to the parcel a member in the class which is an ArrayList<Parcelable>
object. The ArrayList
is Serializable
, and the objects (ZigBeeDev
) in the list are Parcelable
.
Here is the relevant code:
package com.gnychis.coexisyst;
import java.util.ArrayList;
import java.util.Iterator;
import android.os.Parcel;
import android.os.Parcelable;
public class ZigBeeNetwork implements Parcelable {
public String _mac; // the source address (of the coordinator?)
public String _pan; // the network address
public int _band; // the channel
ArrayList<Integer> _lqis; // link quality indicators (to all devices?)
ArrayList<ZigBeeDev> _devices; // the devices in the network
public void writeToParcel(Parcel out, int flags) {
out.writeString(_mac);
out.writeString(_pan);
out.writeInt(_band);
out.writeSerializable(_lqis);
out.writeParcelable(_devices, 0); // help here
}
private ZigBeeNetwork(Parcel in) {
_mac = in.readString();
_pan = in.readString();
_band = in.readInt();
_lqis = (ArrayList<Integer>) in.readSerializable();
_devices = in.readParcelable(ZigBeeDev.class.getClassLoader()); // help here
}
public int describeContents() {
return this.hashCode();
}
public static final Parcelable.Creator<ZigBeeNetwork> CREATOR =
new Parcelable.Creator<ZigBeeNetwork>() {
public ZigBeeNetwork createFromParcel(Parcel in) {
return new ZigBeeNetwork(in);
}
public ZigBeeNetwork[] newArray(int size) {
return new ZigBeeNetwork[size];
}
};
//...
}
I have marked two spots "// help here" to understand how to properly write to the parcel and how to reconstruct it. If ZigBeeDev
is Parcelable
(properly tested), how do I do this properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你差点就明白了!
你只需要做:
仅此而已!
对于您的整数列表,您还可以执行以下操作:
它应该有效。
You almost got it !
You just need to do :
That's all!
For your list of Integer, you can also do :
It should work.
就我而言,
in.readTypedList(_devices, ZigBeeDev.CREATOR);
在_devices
上给了我一个NullPointerException
。所以我用了这个:In my case
in.readTypedList(_devices, ZigBeeDev.CREATOR);
gave me aNullPointerException
on_devices
. So I used this:您应该使用 writeList(List l)整数列表和 writeTypedList(List val)< /a> 对于ZigBee 设备列表
You should use writeList(List l) for your list of integers and writeTypedList(List val) for the list of ZigBeeDevices
在构造函数中,您应该使用
在“writeToParcel”中使用
In constructor you should use
And in "writeToParcel" use
有点晚了,但我也遇到了这个问题。在浪费了很长时间之后,我偶然发现了 parcelabler.com 网站,它会自动为您创建包裹。
它使我能够非常轻松地创建一个内部包含数组列表的嵌套包裹,并节省了我很多时间。
基本上,该网站的工作方式是您输入带有 ArrayList 的对象,它会自动添加必要的方法以使其可打包(从 Parcel 读取、写入 Parcel、描述内容和 Parcel Creator 都是自动生成的)。这在创建复杂地块时特别有用,例如这里提出的问题,其中包含嵌套地块、数组和列表。
编辑:IntelliJ IDEA 和 Android Studio 也有插件,其功能与列出的网站类似:
这些插件生成 Android Parcelable 基于类中字段的样板代码。
A little late but I also had this issue. After a long waste of time I stumbled upon the parcelabler.com website which automatically creates parcels for you.
It allowed me to create a nested parcel with an array list inside very easily, and saved me a lot of time.
Basically the way the website works is you enter your object with the ArrayList in it and it automatically adds the necessary methods to make it parcelable (read from parcel, write to parcel, describe contents, and parcel Creator are all generated automatically). This is particularily useful when creating complex parcels, such as the question here asks, which contain nested parcels, arrays, and lists.
EDIT: Also IntelliJ IDEA and Android Studio have plugins for this which do a similar thing to the website listed:
These plugins generate Android Parcelable boilerplate code based on fields in the class.