gpt4 book ai didi

java - Parcelable 类不会创建可分割对象

转载 作者:行者123 更新时间:2023-12-02 10:23:20 25 4
gpt4 key购买 nike

我正在尝试在 bundle 中保存对象的 ArrayList(列表架子项),以便下次打开 Activity 时检索它。

(该 Activity 从 firestore 获取信息,我想减少读取并减少每次打开该 Activity 时的加载时间)。

但我收到此错误消息:

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

最佳答案

在学习 Java 时,我们被教导使用接口(interface)类型(List)而不是实现类型( ArrayList)当我们声明变量时。您可能在某处有如下所示的代码:

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

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

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

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

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

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

关于java - Parcelable 类不会创建可分割对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54161810/

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