如何在可拆分的Android Studio Java中传递具有抽象类类型作为参数的类

时间:2018-08-01 03:54:04

标签: java android parcelable

我正在尝试通过此类作为android中的包裹。

public class Outfit implements Parcelable {

    private List<Item> itemList;
    private String mName;
    private String mImageUrl;

    public Outfit() {}

    public Outfit(String mName, String mImageUrl) {
        this.mName = mName;
        this.mImageUrl = mImageUrl;

    }

    protected Outfit(Parcel in) {
        itemList = in.createTypedArrayList(Item.CREATOR);
        mName = in.readString();
        mImageUrl = in.readString();
    }

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

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

    public String getmName() {
        return mName;
    }

    public String getmImageUrl() {
        return mImageUrl;
    }

    @Override
    public String toString() {
        return mName;
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeTypedList(itemList);
        parcel.writeString(mName);
        parcel.writeString(mImageUrl);
    }
}

问题是Item是抽象的,而itemList = in.creatTypedArrayList(Item.CREATOR),因为Item没有CREATOR。只有其子类具有此实现。

Item.java

public abstract class Item implements Parcelable {

    private String mName;

    private String mColor;

    private String mImageUrl;

    private List<TagHolder> tags = new ArrayList<>();

    private String mKey;

    public Item(){

    }

    public Item(String mName, String mColor, String mImageUrl) {
        this.mName = mName;
        this.mColor = mColor;
        this.mImageUrl = mImageUrl;
    }

    protected Item(Parcel in) {
        mName = in.readString();
        mColor = in.readString();
        mImageUrl = in.readString();
        tags = in.createTypedArrayList(TagHolder.CREATOR);
        mKey = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mName);
        dest.writeString(mColor);
        dest.writeString(mImageUrl);
        dest.writeTypedList(tags);
        dest.writeString(mKey);
    }

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


    public void setmName(String mName) {
        this.mName = mName;
    }

    public void setmImageUrl(String mImageUrl) {
        this.mImageUrl = mImageUrl;
    }

    public List<TagHolder> getTags() {
        return tags;
    }

    public String getmColor() {
        return mColor;
    }

    public String getmImageUrl() {
        return mImageUrl;
    }

    public void setmColor(String mColor) {
        this.mColor = mColor;
    }

    public String getmName() {
        return mName;
    }

    public void setTags(List<TagHolder> tags) {
        this.tags = tags;
    }

    @Exclude // dont need this in our firebase database
    public String getKey() {
        return mKey;
    }

    @Exclude
    public void setMkey(String key) {
        mKey = key;
    }

    public abstract String getCategory();
}

当我打电话给多余的东西时,我能够将List拆分为可拆分数组。但是,当我尝试这样做时,会出现错误。有什么办法可以将服装作为包裹传递出去吗?

1 个答案:

答案 0 :(得分:2)

您是否尝试使用另一种方法创建列表?

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeList(itemList);
    parcel.writeString(mName);
    parcel.writeString(mImageUrl);
}

protected Outfit(Parcel in) {
    in.readList(itemList, Item.class.getClassLoader());
    mName = in.readString();
    mImageUrl = in.readString();
}