Parcelable class 不生成可打包对象

Parcelable class does not make a parcelable object

我试图在包中保存一个对象数组列表 (List shelfItems),以便下次打开 activity 时检索它。

(activity 从 firestore 获取信息,我想在每次打开 activity 时减少读取并减少加载时间。

但我收到此错误消息:

savedInstanceState.putParcelableArrayList("key", shelfItems);

"putParcelableArrayList(java.lang.String, java.util.ArrayList)' in 'android.os.Bundle' cannot be applied to '(java.lang.String, java.util.List)"

这是我的对象class:

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

在学习 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);
}