是否可以打包通用类? [英] Is it possible to parcel a generic class?

查看:94
本文介绍了是否可以打包通用类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建public class MyClass<T extends Parcelable> implements Parcelable.我在实现Parcelable时遇到问题.是否可以创建实现Parcelable的泛型类? (请注意,T是有界的,因此它也必须实现Parcelable).

I'm trying to create public class MyClass<T extends Parcelable> implements Parcelable. I'm having trouble implementing Parcelable. Is it possible to create a generic class that implements Parcelable? (Note that T is bounded so that it also must implement Parcelable).

我对Parcelable接口需要一个 static 变量:public static final Parcelable.Creator<MyParcelable> CREATOR感到困扰.因此,我不能执行public static final Parcelable.Creator<MyClass<T>> CREATOR,因为MyParcelable<T>是非静态的.

I am running into trouble with the fact that the Parcelable interface requires a static variable: public static final Parcelable.Creator<MyParcelable> CREATOR. Thus I cannot do public static final Parcelable.Creator<MyClass<T>> CREATOR because MyParcelable<T> is nonstatic.

安德烈

推荐答案

我在使用泛型的类上实现Parcelable时遇到类似的问题,第一个问题与您遇到的问题相同:

I had similar issues with implementing Parcelable on a class with a generic, the first issue was the same as what you were experiencing:

因此,我无法执行公共静态最终Parcelable.Creator> CREATOR,因为MyParcelable是非静态的.

Thus I cannot do public static final Parcelable.Creator> CREATOR because MyParcelable is nonstatic.

第二个是读取需要访问ClassLoader的Parcelable对象,由于

The second was to read in a Parcelable object you need access to the ClassLoader which cannot be gotten from T due to type erasure.

下面的类是我在生产中使用的类的改编版,它克服了两个问题. 注意:我还没有专门测试这个类,所以如果您有任何问题,请告诉我.

The class below is an adaption of a class I am using in production which overcomes both issues. Note: I have not tested this class specifically, so let me know if you have any issues.

public class TestModel<T extends Parcelable> implements Parcelable {

private List<T> items;
private String someField;

public List<T> items() {
    return items;
}

public void setItems(List<T> newValue) {
    items = newValue;
}

public String someField() {
    return someField;
}

public void setSomeField(String newValue) {
    someField = newValue;
}

//region: Parcelable implementation

public TestModel(Parcel in) {
    someField = in.readString();

    int size = in.readInt();
    if (size == 0) {
        items = null;
    }

    else {

        Class<?> type = (Class<?>) in.readSerializable();

        items = new ArrayList<>(size);
        in.readList(items, type.getClassLoader());
    }
}

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

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

    if (items == null || items.size() == 0)
        dest.writeInt(0);

    else {
        dest.writeInt(items.size());

        final Class<?> objectsType = items.get(0).getClass();
        dest.writeSerializable(objectsType);

        dest.writeList(items);
    }
}

public static final Parcelable.Creator<TestModel> CREATOR = new Parcelable.Creator<TestModel>() {
    public TestModel createFromParcel(Parcel in) {
        return new TestModel(in);
    }

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

//endregion
}

这篇关于是否可以打包通用类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆