gpt4 book ai didi

java - 如何在parcelable android studio java中传递一个具有抽象类类型的类作为参数

转载 作者:行者123 更新时间:2023-11-29 18:46:40 25 4
gpt4 key购买 nike

我正在尝试将此类作为 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 。只有它的子类有这个实现。

项目.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();
}

当我调用 put extra 时,我能够将一个列表作为一个可打包的数组进行打包。但是当我尝试为装备这样做时,它会出错。有什么办法可以将 Outfit 作为包裹传递吗?

最佳答案

您是否尝试对列表使用其他方法?

@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();
}

关于java - 如何在parcelable android studio java中传递一个具有抽象类类型的类作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51625116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com