java - Parcelable writeToParcel(): What's the best way to write multiple variables of the same type? -
i have 10 strings: str1, str2, str3...,str10 (not actual string names). have dest.writestring(str_n) 10 strings or there easier way this? how read them in?
example:
@override public void writetoparcel(parcel dest, int flags) { dest.writestring(str1); dest.writestring(str2); dest.writestring(str3); dest.writestring(str4); dest.writestring(str5); dest.writestring(str6); dest.writestring(str7); dest.writestring(str8); dest.writestring(str9); dest.writestring(str10); }
as can see, become lengthy. suggestions help! thanks!
if have 10 strings in parcelable
class , want restore values, that's way it. read them in creating parcelable.creator
, private constructor accepts parcel object parameter, in constructor phone call readstring()
on parcel in same order called writestring()
. see http://developer.android.com/reference/android/os/parcelable.html
it this:
public class myparcelable implements parcelable { private string str1; private string str2; private string str3; private string str4; private string str5; private string str6; private string str7; private string str8; private string str9; private string str10; public int describecontents() { homecoming 0; } public void writetoparcel(parcel dest, int flags) { dest.writestring(str1); dest.writestring(str2); dest.writestring(str3); dest.writestring(str4); dest.writestring(str5); dest.writestring(str6); dest.writestring(str7); dest.writestring(str8); dest.writestring(str9); dest.writestring(str10); } public static final parcelable.creator<myparcelable> creator = new parcelable.creator<myparcelable>() { public myparcelable createfromparcel(parcel in) { homecoming new myparcelable(in); } public myparcelable[] newarray(int size) { homecoming new myparcelable[size]; } }; private myparcelable(parcel in) { str1 = in.readstring(); str2 = in.readstring(); str3 = in.readstring(); str4 = in.readstring(); str5 = in.readstring(); str6 = in.readstring(); str7 = in.readstring(); str8 = in.readstring(); str9 = in.readstring(); str10 = in.readstring(); } }
java android object parcelable parcel
No comments:
Post a Comment