4

I have ten strings: str1, str2, str3...,str10 (not actual string names). Do I have to do dest.writeString(str_n) for all 10 strings or is there an easier way to do this? How would I read them back 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 you can see, this could become very lengthy. Any suggestions would help! Thanks!

kyrax
  • 1,192
  • 3
  • 13
  • 29

2 Answers2

15

If you have 10 strings in your Parcelable class and you want to restore their values, then that's the way to do it. You read them back in by creating a Parcelable.Creator and a private constructor that accepts a Parcel object as its parameter, then in that constructor you call readString() on the Parcel in the same order you called writeString(). See http://developer.android.com/reference/android/os/Parcelable.html

It would look something like 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() {
         return 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) {
             return new MyParcelable(in);
         }

         public MyParcelable[] newArray(int size) {
             return 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();       
     }
 }
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
Doug Simonton
  • 1,021
  • 8
  • 14
1

For writing them, you could do the shortcut as suggested to write a string array. Reading them back in, however, you're still going to have to assign them back one by one.

You could write a couple of utility methods:

public static void writeStrings(Parcel out, String... strings) {
    out.writeInt(strings.length);
    for (String string : strings) {
        out.writeString(string);
    }
}

public static String[] readStrings(Parcel in) {
    final String[] strings = new String[in.readInt()];
    for (int i = 0; i < strings.length; i++) {
        strings[i] = in.readString();
    }
    return strings;
}

and then to write them:

writeStrings(dest, str1, str2, str3, str4, str5);

and to read them:

String[] strings = readStrings(dest);
str1 = strings[0];
str2 = strings[1];
str3 = strings[2];
str4 = strings[3];
str5 = strings[4];

Honestly, just for maintainability and readability, I'd suggest avoiding doing anything too clever here and just write them out one by one. Parcelable is a nice mechanism; unfortunately, there's just not much you can do to reduce the boilerplate for cases like this.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274