如何在Java中Parcelable类中包裹对象类型

时间:2021-01-09 19:40:39

How this class implements parcelable right. i cant do parcel Object type.String, Double,Integer,.. variables types sets to the Object type.

这个类如何实现parcelable权利。我不能做parcel对象类型.String,Double,Integer,..变量类型设置为Object类型。

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.
    }
}

1 个解决方案

#1


0  

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.

您需要定义一个为您的值实现Parcelable的类。

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). 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.*
  • 任何实现Serializable的对象(但请参阅writeSerializable(Serializable)以获取警告)。请注意,所有以前的类型都具有相对有效的写入包的实现;不得不依赖通用序列化方法效率低得多,应尽可能避免使用。*

For the complete list, see the doc of the method

有关完整列表,请参阅该方法的文档

#1


0  

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.

您需要定义一个为您的值实现Parcelable的类。

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). 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.*
  • 任何实现Serializable的对象(但请参阅writeSerializable(Serializable)以获取警告)。请注意,所有以前的类型都具有相对有效的写入包的实现;不得不依赖通用序列化方法效率低得多,应尽可能避免使用。*

For the complete list, see the doc of the method

有关完整列表,请参阅该方法的文档