Android Parcelable决赛

Minhaz

所以,我有以下课程,我想使用Android Parcelable进行序列化

public class Parent{
   public static final int x; //primtive type
   public static final Animal y; //another percelable
   public static final String z; //object type
   public class Inner{
       public static final int a; //primtive type
       public static final Animal b; //another percelable
       public static final String c; //object type
   }
   private int classIntState; //primtive type
   private Animal classAnimalState; //another percelable
   private String classObjectState; //object type
}

我知道类变量的练习。但是在决赛和内部类的决赛中遇到麻烦。

做了一些额外的研究,我研究了BlutoothDevice类,它恰好也是Parcelable并具有一堆常量。

而且,我在源代码中发现了这一点,

    public static final Parcelable.Creator<BluetoothDevice> CREATOR =
        new Parcelable.Creator<BluetoothDevice>() {
    public BluetoothDevice createFromParcel(Parcel in) {
        return new BluetoothDevice(in.readString());
    }
    public BluetoothDevice[] newArray(int size) {
        return new BluetoothDevice[size];
    }
   };

   public void writeToParcel(Parcel out, int flags) {
       out.writeString(mAddress);
   }

   BluetoothDevice(String address) {
        getService();  // ensures sService is initialized [nothing to do with parcelable]
        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
            throw new IllegalArgumentException(address + " is not a valid Bluetooth address");
        }

        mAddress = address;
    }

似乎这些家伙完全忽略了其Parcelable实现中的所有那些finals / Constants。

这让我有些困惑,因此研究了Java序列化决赛的方式,并在堆栈溢出时找到了此讨论我从他们那里了解到的是,它是由JVM使用反射完成的。然后,我查看了ParcelParcelable的源代码,似乎在任何地方都不明显地使用了final句柄。

我必须还是不需要?我在这里真的想念什么?

u3l

似乎这些家伙完全忽略了其Parcelable实现中的所有那些finals / Constants。

好吧,如果你看一下BluetoothDevice,你会发现,所有的final变量都在构造函数之外定义。因此,在return new BluetoothDevice(in.readString())调用时,构造函数定义之外所有final变量都将初始化为它们各自的值,然后调用构造函数BluetoothDevice(String address)

我要说的是,这些值不是从写入或读取的Parcel它们可以在CREATORof调用构造函数之前由类简单地初始化Parent(尽管您尚未在问题的Parent源代码中定义一个)。

现在,假设您final基于参数化构造函数中的参数初始化变量的值例如,

public Parent(int x, Animal y, String z) {

  this.x = x;
  this.y = y;
  this.z = z;

}

在这种情况下,你需要写xyzParcelwriteToParcel(),就像你将任何non-final价值。

然后在您的Parent课堂上' CREATOR

public static final Parcelable.Creator<Parent> CREATOR =
    new Parcelable.Creator<Parent>() {
    public Parent createFromParcel(Parcel in) {

        //read values from Parcel
        int intParam = in.readInt();
        Animal animalParam = in.readParcelable(Animal.class.getClassLoader());
        String stringParam = in.readString();

        //create parent
        Parent parent = new Parent(intParam, animalParam, stringParam);

        return parent;
    }
    public Parent[] newArray(int size) {
        return new Parent[size];
    }
 };

也就是说,您读取变量的值,然后使用构造函数分配它们。同样,这几乎与变量not几乎相同final我认为它们之间没有任何区别。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android abstract parcelable class

来自分类Dev

Kotlin中的Android Parcelable

来自分类Dev

Android Parcelable与SQLite

来自分类Dev

Android Parcelable String null

来自分类Dev

Android Parcelable Arraylist

来自分类Dev

带有JsonObject的Android Parcelable

来自分类Dev

Android Parcelable投放错误

来自分类Dev

Android:将Parcelable转换为JSON

来自分类Dev

Android Parcelable错误数组长度

来自分类Dev

Android Parcelable readStringArray错误的数组长度

来自分类Dev

Android:Parcelable不返回任何内容

来自分类Dev

Android Parcelable readStringArray错误的数组长度

来自分类Dev

Android:将Parcelable转换为JSON

来自分类Dev

侧面JsonObject中的Android Parcelable JsonArray

来自分类Dev

无需getter和setter的Android Parcelable实现

来自分类Dev

Kotlin中的Android Parcelable:在Parcelable数据类上找不到CREATOR

来自分类Dev

关于公开决赛无效

来自分类Dev

解组Android Intent Parcelable时找不到类

来自分类Dev

Android-实施Parcelable的更简单/更快/更少的乏味方法?

来自分类Dev

Android实现具有哈希图的Parcelable对象

来自分类Dev

android.os.Parcelable []无法转换为...错误

来自分类Dev

无法使用Android中Parcelable类中归档的日期

来自分类Dev

Android Parcelable对象在活动之间无法正确传递

来自分类Dev

在android中实现Parcelable时,不会调用onRestoreInstanceState

来自分类Dev

Android Parcelable类-内部类不能具有静态声明

来自分类Dev

如何从Android中的Parcelable Object中提取数据

来自分类Dev

为什么“班级”班级决赛?

来自分类Dev

putParcelableArrayListExtra(ArrayList <Uri>)引发ArrayList无法转换为android.os.Parcelable

来自分类Dev

Android:如何使用Class <?将Activity>扩展为Parcelable中的变量类型

Related 相关文章

热门标签

归档