r1k4r2
r1k4r2

Reputation: 75

how to parcel hashset with custom objects

i'm implementing an application in android and i need to make my objets parcelables to move between activities. I use hibernate in the server so my classes have hashsets as atribute and i don't know how to parcel.

Here is my code and my trying... but i don't know if its ok and i don't know to do el write to parcel. Also i want to know if i did well the parcel for DATE and my other objets as atributes

public class Incidencia implements Parcelable {

// Atributos
private String id_incidencia;
private String estado;
private double latitud;
private double longitud;
private Date fecha_creacion;
private Date fecha_cierre;

private Puesto puesto;
private SubTipo subtipo;

private Set<iTarea> iTareas = new HashSet<iTarea>();

// Constructores
public Incidencia() {}

public Incidencia(String id, String est, double lat, double lon, Date f_cre, Date f_cie, Puesto pue, SubTipo sub, Set<iTarea> iTa) {
    this.id_incidencia = id;
    this.estado = est;
    this.latitud = lat;
    this.longitud = lon;
    this.fecha_creacion = f_cre;
    this.fecha_cierre = f_cie;
    this.puesto = pue;
    this.subtipo = sub;
    this.iTareas = iTa;
}

// Necesarios para que sea Parcelable
public Incidencia(Parcel in) {
    this.id_incidencia = in.readString();
    this.estado = in.readString();
    this.latitud = in.readDouble();
    this.longitud = in.readDouble();
    this.fecha_creacion = new Date(in.readLong());
    this.fecha_cierre = new Date(in.readLong());
    this.puesto = (Puesto)in.readParcelable(Puesto.class.getClassLoader());
    this.subtipo = (SubTipo)in.readParcelable(SubTipo.class.getClassLoader());
    this.iTareas = (Set<iTarea>) in.readHashMap(iTarea.class.getClassLoader());

}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.getId_incidencia());
    dest.writeString(this.getEstado());
    dest.writeDouble(this.getLatitud());
    dest.writeDouble(this.getLongitud());
    dest.writeLong(this.getFecha_creacion().getTime());
    dest.writeLong(this.getFecha_cierre().getTime());
    dest.writeParcelable((Parcelable) this.puesto, flags);
    dest.writeParcelable((Parcelable) this.subtipo, flags);
}

public int describeContents() {
    return 0;
}

public static final Parcelable.Creator<Incidencia> CREATOR = new Parcelable.Creator<Incidencia>() {
    public Incidencia createFromParcel(Parcel in) {
        return new Incidencia(in);
    }

    public Incidencia[] newArray(int size) {
        return new Incidencia[size];
    }
};

Upvotes: 4

Views: 6036

Answers (1)

ebarrenechea
ebarrenechea

Reputation: 3785

I'm fairly certain it won't work, mainly because a Set is not a Map. A better approach would be for you to get an array of objects from your set using the Set.toArray method and use the Parcel.writeTypedArray to write it to the parcel. Then you can just read the array and populate the Set yourself when recreating the object. This would require that your iTarea class implement Parcelable as well, but it is a much better solution then trying to pass a HashSet off as a HashMap.

Your other objects and dates should be fine, by the way.

Upvotes: 14

Related Questions