android - Nested parcelable object not reading -
i'm trying parcel object contains string/int variables , object variable. strings , int's working, not nested object. understand have parcelable, i'm apparently doing wrong=. in nested class, writetoparcel
method gets called (i check log.d()
call), createfromparcel()
doesn't. end getting null
object. simplified code:
public class myclass implements parcelable { public myclass() { } private integer id; private string name; private myotherclass otherclass = new myotherclass(); public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public otherclass getotherclass() { return otherclass; } public void setotherclass(otherclass otherclass) { this.otherclass = otherclass; } public static final parcelable.creator<myclass> creator = new parcelable.creator<myclass>() { public myclass createfromparcel(parcel in) { return new myclass(in); } public myclass[] newarray(int size) { return new myclass[size]; } }; @override public int describecontents() { // todo auto-generated method stub return 0; } @override public void writetoparcel(parcel dest, int flags) { dest.writestring(name); dest.writeint(id); dest.writeparcelable(otherclass, flags); } private myclass(parcel in) { name = in.readstring(); id = in.readint(); otherclass = (otherclass) in.readparcelable(otherclass.class.getclassloader()); } } class myotherclass implements parcelable { public otherclass() { } private string resourcepreviewurl; public string getresourcepreviewurl() { return resourcepreviewurl; } public void setresourcepreviewurl(string resourcepreviewurl) { this.resourcepreviewurl = resourcepreviewurl; } public int describecontents() { return 0; } public void writetoparcel(parcel out, int flags) { log.d("parcel", "write parcel"); // gets called out.writestring(resourcepreviewurl); } public static final parcelable.creator<myotherclass> creator = new parcelable.creator<myotherclass>() { public myotherclass createfromparcel(parcel in) { log.d("parcel", "create parcel"); // doesn't called return new myotherclass(in); } public resourcepreviews[] newarray(int size) { return new resourcepreviews[size]; } }; private otherclass(parcel in) { log.d("parcel", "read parcel"); // doesn't called resourcepreviewurl = in.readstring(); } }
i solved changing order in write/read parcel. made dest.writeparcelable(otherclass, flags);
, otherclass = (otherclass) in.readparcelable(otherclass.class.getclassloader());
calls first in methods , started working. issue?
@override public void writetoparcel(parcel dest, int flags) { dest.writeparcelable(otherclass, flags); dest.writestring(name); dest.writeint(id); } private myclass(parcel in) { otherclass = (otherclass) in.readparcelable(otherclass.class.getclassloader()); name = in.readstring(); id = in.readint(); }
Comments
Post a Comment