简体   繁体   English

如何在 Java 中的 Parcelable 类中打包对象类型

[英]how to parcel Object type in Parcelable class in Java

How this class implements parcelable right.这个类如何实现可分割的权利。 i cant do parcel Object type.String, Double,Integer,.. variables types sets to the Object type.我不能做包裹对象类型。字符串,双,整数,..变量类型设置为对象类型。

public class MyClass implements Parcelable {
    public String Name;
    public Object Value; //variables type to set

    protected MyClass(Parcel in) {
        Name = in.readString();
        //Value = in.?
    }

    public static final Creator<MyClass> CREATOR = new Creator<MyClass>() {
        @Override
        public MyClass createFromParcel(Parcel in) {
            return new MyClass(in);
        }

        @Override
        public MyClass[] newArray(int size) {
            return new MyClass[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(Name);
        //dest.
    }
}

Probably I am too late but this might help someone else looking for same.可能我为时已晚,但这可能会帮助其他人寻找相同的东西。

Since the Object class does not implement Parcelable, a workaround is needed.由于 Object 类没有实现 Parcelable,因此需要一种解决方法。 A way which i got through this obstacle was by using GSON and serializing and de-serializing to String and from String.我克服这个障碍的一种方法是使用 GSON 并将序列化和反序列化为字符串和从字符串。

For eg:例如:

public class MyClass implements Parcelable {
    public String Name;
    public Object Value; //variables type to set

    protected MyClass(Parcel in) {
        Name = in.readString();
        Value = new Gson().fromJson(in.readString(), Object.class);
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(Name);
        dest.writeString(new Gson().toJson(Value));
    }

}
  • When writing to parcel converting the Object (Serializing) using GSON to JSON String and writing it as a String to parcel.写入包裹时,使用 GSON 将对象(序列化)转换为 JSON 字符串并将其作为字符串写入包裹。
  • While reading from parcel reading the parcel as String and converting (Deserializing) it to Object class using Gson again.从包裹读取时,将包裹作为字符串读取并再次使用 Gson 将其转换(反序列化)为 Object 类。

The Parcel documentation is quite explicit about what you can do, here is some idea. Parcel文档非常明确地说明了您可以做什么,这里有一些想法。

You have the methods writeParcelable to do that, but you can't parcel an Object because Object doesn't implement Parcelable .你有方法writeParcelable来做到这一点,但你不能打包一个Object因为Object没有实现Parcelable

You need to define a class that implements Parcelable for your value .您需要定义一个为您的value实现Parcelableclass

public class MyClass implements Parcelable {
    public String name;
    public ParcelableValue value; //variables type to set
    ...

    
}

public class ParcelableValue implements Parcelable {
    ...
}

For a complete implementation, see Write a sub class of Parcelable to another Parcel完整的实现见将 Parcelable 的子类写到另一个 Parcel

You also have writeValue but this still have some condition :你也有writeValue但这仍然有一些条件:

  • Any object that implements Serializable (but see writeSerializable(Serializable) for caveats).任何实现 Serializable 的对象(但请参阅 writeSerializable(Serializable) 以了解注意事项)。 Note that all of the previous types have relatively efficient implementations for writing to a Parcel;请注意,所有以前的类型都有相对高效的写入包的实现; having to rely on the generic serialization approach is much less efficient and should be avoided whenever possible.*必须依赖通用序列化方法效率低得多,应尽可能避免。*

For the complete list, see the doc of the method有关完整列表,请参阅该方法的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Parcelable 对象列表的 Parcel List - Parcel List of List of Parcelable object 如何将包含可包裹类“ B”的实例的可包裹类“ A”的实例写入类“ B”的包裹 - How to write an instance of Parcelable class 'A' which contains an instance of parcelable class 'B', to Parcel from class 'B' Parcelable类中的Parcelable列表不使用Parcel.readTypedList进行读回 - Parcelable list inside Parcelable class not reading back with Parcel.readTypedList 如何在可拆分的Android Studio Java中传递具有抽象类类型作为参数的类 - How to pass a class with an abstract class type as parameter in parcelable android studio java 如何通过通用类使对象可包裹 - How to make an object parcelable through a generic class 如何在 Android 中将 Java Socket 对象添加到 Parcelable - How to add Java Socket object to Parcelable in Android Android Parcelable-使用通用数据类型将数据读取/写入Parcel - Android Parcelable - read/write data to Parcel using generic data type 可包裹类不构成可包裹对象 - Parcelable class does not make a parcelable object 将 GSON 用于通用 Parcelable——如何检索类类型 - Using GSON for generic Parcelable -- how to retrieve class type 如何将Parcelable用于类中的自定义类型对象/变量(Android) - how to use Parcelable for custom type objects/variable in a class (Android)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM