如何从parcelable正确读取序列化?

时间:2011-07-29 20:34:09

标签: java serialization parcelable

我在Parcelable对象中有一个可序列化的对象(ArrayList)。我正在尝试阅读可序列化的对象,我无法弄清楚如何在阅读后重新构建它:

public class WifiAP implements Parcelable {

public String _mac;
public String _mac2;
public String _ssid;
public boolean _dualband;
public int _band;  // kilohertz
public int _band2;
ArrayList<Integer> _rssis;

public Packet _beacon;

public void writeToParcel(Parcel out, int flags) {
    out.writeString(_mac);
    out.writeString(_mac2);
    out.writeString(_ssid);

    if(_dualband)
        out.writeInt(1);
    else
        out.writeInt(0);

    out.writeInt(_band);
    out.writeInt(_band2);
    out.writeSerializable(_rssis);
}

private WifiAP(Parcel in) {
    _mac = in.readString();
    _mac2 = in.readString();
    _ssid = in.readString();

    if(in.readInt()==1)
        _dualband=true;
    else
        _dualband=false;

    _band = in.readInt();
    _band2 = in.readInt();
    _rssis = in.readSerializable(); // help here, this throws an error
}

如何从Parcel.readSerializable正确地重新创建ArrayList?

1 个答案:

答案 0 :(得分:4)

尝试

   _rssis = (ArrayList<Integer>)  in.readSerializable();