android - How to create Parcelable code for object that contains GregorianCalendar -
android - How to create Parcelable code for object that contains GregorianCalendar -
i haven't understood how create code needed implement correctly parcelable
object contains gregoriancalendar
objects.
e.g. object user
contains string name;
, gregoriancalendar creationdate;
, effort this:
@override public int describecontents() { homecoming 0; } @override public void writetoparcel(parcel dest, int flags) { dest.writestring(this.name); dest.writeparcelable(this.creationdate, flags); } private user(parcel in) { this.name = in.readstring(); this.creationdate = in.readparcelable(gregoriancalendar.class.getclassloader()); } public static final creator<user> creator = new creator<user>() { public user createfromparcel(parcel source) { homecoming new user(source); } public user[] newarray(int size) { homecoming new user[size]; } };
that unfortunately doesn't work
in writetoparcel()
@ line
dest.writeparcelable(this.creationdate, flags);
get writeparcelable cannot applied gregoriancalendar error
in
this.creationdate = in.readparcelable(gregoriancalendar.class.getclassloader());
get incompatible types error
how code correctly parcelable
?
edit
i have tried code generators utilize different ways , i'm not sure right implementation, first 1 utilize writevalue , readvalue in way:
@override public void writetoparcel(parcel dest, int flags) { dest.writestring(name); dest.writevalue(creationdate); } protected user(parcel in) { name = in.readstring(); creationdate = (gregoriancalendar) in.readvalue(gregoriancalendar.class.getclassloader()); }
the sec 1 use
@override public void writetoparcel(parcel dest, int flags) { dest.writestring(name); dest.writeserializable(creationdate); } protected user(parcel in) { name = in.readstring(); creationdate = (gregoriancalendar) in.readserializable(); }
what right way?
you utilize sec way since gregoriancalendar
implements serializable
, , parcelable
work.
@override public void writetoparcel(parcel dest, int flags) { dest.writestring(name); dest.writeserializable(creationdate); } protected user(parcel in) { name = in.readstring(); creationdate = (gregoriancalendar) in.readserializable(); }
however you must not serialize gregoriancalendar, because updates in gregoriancalendar
related class can cause issue. consider case load file contains gregoriancalendar
objects created different api version, difference in gregoriancalendar
implementation lead sure errors, plenty little difference in serialversionuid
costant prevent right load of file.
use long
parameters store millisecond of date, if don't want rewrite whole application can create couple of methods convert millis gregoriancalendar
, vice-versa need.
android parcelable parcel
Comments
Post a Comment