如何使用ArrayList 正确实现Parcelable?

时间:2022-09-02 11:03:24

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.

我无法让我的课程Parcelable。麻烦的是,我试图写一个包中的一个成员,这是一个ArrayList 对象。 ArrayList是Serializable,列表中的对象(ZigBeeDev)是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?

我已经标记了两个“// help here”,以了解如何正确地写入包裹以及如何重建它。如果ZigBeeDev是Parcelable(经过适当测试),我该如何正确地做到这一点?

4 个解决方案

#1


134  

You almost got it !

你几乎得到了它!

You just need to do :

你只需要这样做:

public void writeToParcel(Parcel out, int flags) {
    out.writeString(_mac);
    out.writeString(_pan);
    out.writeInt(_band);
    out.writeSerializable(_lqis);
    out.writeTypedList(_devices);
}

private ZigBeeNetwork(Parcel in) {
    _mac = in.readString();
    _pan = in.readString();
    _band = in.readInt();
    _lqis = (ArrayList<Integer>) in.readSerializable();
    in.readTypedList(_devices, ZigBeeDev.CREATOR);
}

That's all!

就这样!

For your list of Integer, you can also do :

对于您的Integer列表,您还可以执行以下操作:

out.writeList(_lqis);
_lqis = new ArrayList<>();
in.readList(_lqis Integer.class.getClassLoader());

It should work.

它应该工作。

#2


17  

In my case in.readTypedList(_devices, ZigBeeDev.CREATOR); gave me a NullPointerException on _devices. So I used this:

在我的例子中,in.readTypedList(_devices,ZigBeeDev.CREATOR);在_devices上给了我一个NullPointerException。所以我用过这个:

_devices = in.createTypedArrayList(ZigBeeDev.CREATOR);

#3


8  

You should use writeList(List l) for your list of integers and writeTypedList(List val) for the list of ZigBeeDevices

你应该使用writeList(List l)作为整数列表,使用writeTypedList(List val)作为ZigBeeDevices列表

#4


2  

In constructor you should use

在构造函数中,您应该使用

_lqis = in.createTypedArrayList(ZigBeeDev.CREATOR);

And in "writeToParcel" use

并在“writeToParcel”中使用

out.writeTypedList(_lqis);

#1


134  

You almost got it !

你几乎得到了它!

You just need to do :

你只需要这样做:

public void writeToParcel(Parcel out, int flags) {
    out.writeString(_mac);
    out.writeString(_pan);
    out.writeInt(_band);
    out.writeSerializable(_lqis);
    out.writeTypedList(_devices);
}

private ZigBeeNetwork(Parcel in) {
    _mac = in.readString();
    _pan = in.readString();
    _band = in.readInt();
    _lqis = (ArrayList<Integer>) in.readSerializable();
    in.readTypedList(_devices, ZigBeeDev.CREATOR);
}

That's all!

就这样!

For your list of Integer, you can also do :

对于您的Integer列表,您还可以执行以下操作:

out.writeList(_lqis);
_lqis = new ArrayList<>();
in.readList(_lqis Integer.class.getClassLoader());

It should work.

它应该工作。

#2


17  

In my case in.readTypedList(_devices, ZigBeeDev.CREATOR); gave me a NullPointerException on _devices. So I used this:

在我的例子中,in.readTypedList(_devices,ZigBeeDev.CREATOR);在_devices上给了我一个NullPointerException。所以我用过这个:

_devices = in.createTypedArrayList(ZigBeeDev.CREATOR);

#3


8  

You should use writeList(List l) for your list of integers and writeTypedList(List val) for the list of ZigBeeDevices

你应该使用writeList(List l)作为整数列表,使用writeTypedList(List val)作为ZigBeeDevices列表

#4


2  

In constructor you should use

在构造函数中,您应该使用

_lqis = in.createTypedArrayList(ZigBeeDev.CREATOR);

And in "writeToParcel" use

并在“writeToParcel”中使用

out.writeTypedList(_lqis);