问题描述
即时实施的Android应用程序,我需要让我的自用物品Parcelables并到活动之间移动。我使用Hibernate的服务器,所以我的班有hashsets作为属性附加伤害,我不知道怎么包裹。
im 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 dont know how to parcel.
下面是我的code和我的努力......但我不知道,如果其确定,我不知道该怎么报写信给包裹。
此外,我想知道,如果我做得很好的包裹日期和我的其他的自用物品atributes
Here is my code and my trying... but i dont know if its ok and i dont 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];
}
};
在此先感谢!
推荐答案
我相当肯定它不会工作,主要是因为设置
不是地图
。更好的办法是,你得到使用 Set.toArray
方法从您所设定的对象的数组,并使用将其写入到包裹。然后,你可以阅读阵列和填充重新创建对象的时候,为自己设定。这要求你的 iTarea
类实现 Parcelable
为好,但它是一个更好的解决方案,然后试图通过一个 HashSet的
断为的HashMap
。
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.
这篇关于如何包裹HashSet的定制自用物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!