java - 可打包 writeToParcel() : What's the best way to write multiple variables of the same type?

标签 java android object parcelable parcel

我有十个字符串:str1、str2、str3...、str10(不是实际的字符串名称)。我是否必须对所有 10 个字符串执行 dest.writeString(str_n) 还是有更简单的方法来执行此操作?我如何读回它们?

示例:

@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);      
}

如您所见,这可能会变得非常冗长。任何建议都会有所帮助!谢谢!

最佳答案

如果您的 Parcelable 类中有 10 个字符串,并且您想要恢复它们的值,那么就可以这样做。您可以通过创建一个 Parcelable.Creator 和一个接受 Parcel 对象作为其参数的私有(private)构造函数来读回它们,然后在该构造函数中对 Parcel 调用 readString() 与调用writeString()的顺序相同。请参阅http://developer.android.com/reference/android/os/Parcelable.html

它看起来像这样:

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();       
     }
 }

关于java - 可打包 writeToParcel() : What's the best way to write multiple variables of the same type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24373281/

相关文章:

Java8 Stream - 来自 IntStream 的字节 HashSet

android - 如何为旧版 Android 浏览器的响应字体实现 vh 和 vw 字体大小单元?

Android Studio 在执行 git merge 时卡住

javascript - 使用 Node JS 将多个数组转换为对象到一个数组

java - IntelliJ IDE 使用空格键而不是 Tab 自动完成功能时遇到问题

java - Eclipse Juno 更新后无法创建服务器

java - Avro:ReflectDatumWriter 不输出架构信息

java - 从 getIntent.getParcelableExtra 中的 TextView 获取值时出错

java - java如何组合列表对象?

JavaScript 对象,两个问题