可包裹类不构成可包裹对象

时间:2019-01-12 16:54:44

标签: java android arraylist parcel

我试图在捆绑中保存一个对象的ArrayList(列表文件项),以在下次打开活动时检索它。

(该活动从Firestore获取信息,我希望减少读取次数,并减少每次打开该活动时的加载时间)。

但我收到此错误消息:

savedInstanceState.putParcelableArrayList("key", shelfItems);

“'android.os.Bundle'中的putParcelableArrayList(java.lang.String,java.util.ArrayList)'无法应用于'(java.lang.String,java.util.List)'

这是我的对象类:

import android.os.Parcel;
import android.os.Parcelable;

public class ShelfItem implements Parcelable{


    private String mTitle;
    private String mAuthor;
    private String mThumbnail;
    private long mRating;
    private long mEndDate;
    private long mBeginDate;
    private String mId;
    private long mPages;
    private boolean mVisible;

    //make ShelfItem object
    public ShelfItem(String title, String author, String thumbnail, long rating, long beginDate, long endDate, String id, long pages, boolean visible) {
        mTitle = title;
        mAuthor = author;
        mThumbnail = thumbnail;
        mRating = rating;
        mBeginDate = beginDate;
        mEndDate = endDate;
        mId = id;
        mPages = pages;
        mVisible = visible;
    }


    public String getTitle() {
        return mTitle;
    }

    public String getAuthor() {
        return mAuthor;
    }

    public String getThumbnail() {
        return mThumbnail;
    }

    public long getRating() {
        return mRating;
    }

    public long getBeginDate() {
        return mBeginDate;
    }

    public long getEndDate() {
        return mEndDate;
    }

    public String getId() {
        return mId;
    }

    public long getPages() {
        return mPages;
    }

    public boolean getVisible() {
        return mVisible;
    }


    public ShelfItem(Parcel in) {
        mId = in.readString();
        mTitle = in.readString();
        mAuthor = in.readString();
        mThumbnail = in.readString();
        mBeginDate = in.readLong();
        mEndDate = in.readLong();
        mPages = in.readLong();
        mVisible = in.readByte() != 0;
        mRating = in.readLong();

    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeString(mId);
        out.writeString(mTitle);
        out.writeString(mAuthor);
        out.writeString(mThumbnail);
        out.writeLong(mBeginDate);
        out.writeLong(mEndDate);
        out.writeLong(mPages);
        out.writeByte((byte) (mVisible ? 1 : 0));
        out.writeLong(mRating);

    }

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

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

}

这是我尝试保存列表的方式:

@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putParcelableArrayList("key", shelfItems);
    }

1 个答案:

答案 0 :(得分:1)

在学习Java时,我们被教导使用接口类型List)而不是实现类型ArrayList)当我们声明变量时。您可能在某处看起来像这样的代码:

List<ShelfItem> shelfItems = new ArrayList<>();

但是,在Bundle和保存列表的特定情况下,您必须专门使用ArrayList,而通常不使用任何List

如果我是对的,并且像上面显示的那样声明了您的列表,只需将其更改为显式使用ArrayList

ArrayList<ShelfItem> shelfItems = new ArrayList<>();

如果您从其他地方获取列表,并且无法控制其实现类型,则可以在需要保存时构造一个new ArrayList

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    ArrayList<ShelfItem> toSave = new ArrayList<>(shelfItems);
    savedInstanceState.putParcelableArrayList("key", toSave);
}