1
如果屏幕旋轉時,我要救我的對象列表,讓我實現我的目標如下:savedInstanceState.getParcelableArrayList()返回空列表
公共類視頻實現Serializable,Parcelable {
private String mTitle;
private String mVideoID;
private String mThumbUrl;
public Video(String title, String id, String thumbUrl) {
this.mTitle = title;
this.mVideoID = id;
this.mThumbUrl = thumbUrl;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(mTitle);
dest.writeString(mVideoID);
dest.writeString(mThumbUrl);
}
public Video(Parcel in) {
// TODO Auto-generated constructor stub
mTitle = in.readString();
mVideoID = in.readString();
mThumbUrl = in.readString();
}
public static final Parcelable.Creator<Video> CREATOR = new Parcelable.Creator<Video>() {
@Override
public Video[] newArray(int size) {
// TODO Auto-generated method stub
return new Video[size];
}
@Override
public Video createFromParcel(Parcel in) {
// TODO Auto-generated method stub
Log.e("CREATOR", in.readString());
return new Video(in);
}
};
對象
}
而且在MainActivity,我取回保存的列表:
if (savedInstanceState != null) {
ArrayList<Video> saved = savedInstanceState.getParcelableArrayList(VIDEO_LIST);
Toast.makeText(this, "Count: " + saved.size(), Toast.LENGTH_SHORT)
.show();
}
- >問題是,旋轉屏幕列表中的對象爲空之後:saved.size()= 0 請告訴我爲什麼?我如何解決它。謝謝
是的,我保存狀態onSaveInstanceState,但我用: outState.putParcelableArrayList(VIDEO_LIST,mVideos); – Binngokute 2014-10-28 16:46:59
看看我的答案更新。 – FelipeJM 2014-10-28 16:49:28
問題是甚至savedInstanceState NOT NULL,但savedInstanceState.getParcelableArrayList(VIDEO_LIST)返回空列表 – Binngokute 2014-10-28 16:50:00