[英]Parcelable class does not make a parcelable object
I am trying to save an ArrayList of objects (List shelfItems) in the bundle to retrieve it next time the activity is opened. 我正在尝试在捆绑包中保存一个对象的ArrayList(列表文件项),以在下次打开活动时检索它。
(the activity gets info from firestore and I want to decrease reads and take away loading time each time the activity is opened). (该活动从Firestore获取信息,而我想减少每次打开活动时的读取次数并减少加载时间)。
but i get this error message: 但我收到此错误消息:
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)" “无法将'android.os.Bundle'中的putParcelableArrayList(java.lang.String,java.util.ArrayList)'应用于'(java.lang.String,java.util.List)
This is my object 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];
}
};
}
and this is how I try to save the list: 这就是我尝试保存列表的方式:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putParcelableArrayList("key", shelfItems);
}
As we learn Java, we're taught to use the interface type ( List
) instead of the implementation type ( ArrayList
) when we declare our variables. 在学习Java时,我们被教导在声明变量时使用接口类型 (
List
)而不是实现类型 ( ArrayList
)。 You probably have code somewhere that looks like this: 您可能在某处看起来像这样的代码:
List<ShelfItem> shelfItems = new ArrayList<>();
However, in the particular case of Bundle
and saving lists, you must use ArrayList
specifically, and not any List
in general. 但是,在
Bundle
和保存列表的特定情况下, 必须专门使用ArrayList
,而通常不使用任何List
。
If I'm right, and your list is declared like I've shown above, just change it to explicitly use ArrayList
: 如果我是对的,并且您的列表的声明如上所示,则将其更改为显式使用
ArrayList
:
ArrayList<ShelfItem> shelfItems = new ArrayList<>();
If you're getting the list from somewhere else, and you can't control the implementation type of it, you can construct a new ArrayList
when you need to save it: 如果要从其他位置获取列表,并且无法控制其实现类型,则可以在需要保存时构造一个
new ArrayList
:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
ArrayList<ShelfItem> toSave = new ArrayList<>(shelfItems);
savedInstanceState.putParcelableArrayList("key", toSave);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.