java - How to create a two dimensional parcelable array of parcelables: -
i have following parcelable object:
public class cgamecard implements parcelable { ... private int mx; private int my; private string mobjectname; private int mstate; public cgamecard(int ax, int ay, string aobjectname) { mx = ax; = ay; mobjectname = aobjectname; mstate = card_state_not_matched; } public int getx() { return mx; } public int gety() { return my; } public string getobjectname(){ return mobjectname; } public int getstate() { return mstate; } ... @override public int describecontents() { return 0; } @override public void writetoparcel(parcel dest, int flags) { dest.writeint(this.mx); dest.writeint(this.my); dest.writestring(this.mobjectname); dest.writeint(this.mstate); } protected cgamecard(parcel in) { this.mx = in.readint(); this.my = in.readint(); this.mobjectname = in.readstring(); this.mstate = in.readint(); } public static final creator<cgamecard> creator = new creator<cgamecard>() { @override public cgamecard createfromparcel(parcel source) { return new cgamecard(source); } @override public cgamecard[] newarray(int size) { return new cgamecard[size]; } }; }
i want create 2 dimensional parcelable array of objects in order store in shared preferences configuration changes. being generated parcelable android studio plugin:
public class gamecardarrarr implements parcelable { private cgamecard[][] marray; public gamecardarrarr(cgamecard[][] array) { marray = array; } public cgamecard[][] getarray() { return marray; } public void setarray(cgamecard[][] marray) { this.marray = marray; } @override public int describecontents() { return 0; } @override public void writetoparcel(parcel dest, int flags) { dest.writeparcelable(this.marray, flags); } protected gamecardarrarr(parcel in) { this.marray = in.readparcelable(cgamecard[][].class.getclassloader()); } public static final creator<gamecardarrarr> creator = new creator<gamecardarrarr>() { @override public gamecardarrarr createfromparcel(parcel source) { return new gamecardarrarr(source); } @override public gamecardarrarr[] newarray(int size) { return new gamecardarrarr[size]; } }; }
which not accepted compiler because not correct.
the question: correct right way create parcelable?
thanks in advance.
first, warn there strict size limit of 1 mb when transferring parcelables around. if double array this, can imagine very easy hit limit , cause crash.
anyway, answer question. can put absolutely want in parcel object needed recreate object. in case, need know 2 dimensions of array, , fill array based on dimensions. have ensure way write them same way read them (which case parcelables).
one method first write how many arrays in double array, write each array.
so in case writetoparcel
this:
void writetoparcel(parcel dest, int flags) { int numofarrays = marray.length; dest.writeint(numofarrays); // save number of arrays (int = 0; < numofarrays; i++) { dest.writeparcelablearray(marray[i], flags); } }
so know first int
number of card arrays in double array.
gamecardarrarr(parcel in) { int numberofarrays = in.readint(); marray = new cgamecard[numberofarays][]; (int = 0; < numberofarrays; i++) { marray[i] = (cgamecard[]) in.readparcelabearray(cgamecard.class.getclassloader()); } }
Comments
Post a Comment