¿cómo implementar adecuadamente Parcelable con un ArrayList?


Estoy teniendo problemas para hacer mi clase Parcelable. El problema es que estoy tratando de escribir al paquete un miembro de la clase que es un objeto ArrayList<Parcelable>. El ArrayList es Serializable, y los objetos (ZigBeeDev) en la lista son Parcelable.

Aquí está el código relevante:

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];
        }
    };

    //...
}

He marcado dos puntos "// ayuda aquí" para entender cómo escribir correctamente en el paquete y cómo reconstruirlo. Si ZigBeeDev es Parcelable (probado correctamente), ¿cómo hago esto correctamente?

Author: Vasily Kabunov, 2011-08-12

4 answers

Casi lo tienes !

Solo tienes que hacer :

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);
}

Eso es todo!

Para su lista de Enteros, también puede hacer:

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

Debería funcionar.

 136
Author: NitroG42,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-31 11:04:55

En mi caso in.readTypedList(_devices, ZigBeeDev.CREATOR); me dio un NullPointerException en _devices. Así que usé esto:

_devices = in.createTypedArrayList(ZigBeeDev.CREATOR);
 17
Author: osrl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-14 19:26:13

Debe usar writeList (List l) para su lista de enteros y writeTypedList (List val) para la lista de ZigBeeDevices

 8
Author: DoubleMalt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-12 15:49:18

En constructor debes usar

_lqis = in.createTypedArrayList(ZigBeeDev.CREATOR);

Y en" writeToParcel " use

out.writeTypedList(_lqis);
 2
Author: Anton Smirnov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-01 08:34:31