I would like to not reload RecyclerView
after screen rotation. I found that I need to store/restore List
from Adapter
. Isn't it ?
But there is problem:
Found java.util.List<Department> requried java.util.ArrayList<?extends android.os.Parcelable>
When I try to put list in the bundle:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("key", mAdapter.getList());
}
mAdapter.getList()
return the List<Department>
from the Adapter of RecyclerView.
But I had implemented Parcelable
inside Department
model class:
@Parcel
public class Department implements UrlInterface,Parcelable {
@SerializedName("department_id")
@Expose
String departmentId;
@SerializedName("short_name")
@Expose
String shortName;
@Expose
String url;
@Expose
HashMap<Integer, Category> categories;
public Department() {}
protected Department(android.os.Parcel in) {
departmentId = in.readString();
shortName = in.readString();
url = in.readString();
}
public static final Creator<Department> CREATOR = new Creator<Department>() {
@Override
public Department createFromParcel(android.os.Parcel in) {
return new Department(in);
}
@Override
public Department[] newArray(int size) {
return new Department[size];
}
};
public String getDepartmentId() { return departmentId; }
public void setDepartmentId(String departmentId) { this.departmentId = departmentId; }
public String getShortName() { return shortName; }
public void setShortName(String shortName) { this.shortName = shortName; }
@Override
public String getUrl() { return url; }
@Override
public String getFullName() { return shortName; }
public void setUrl(String url) { this.url = url; }
public HashMap<Integer, Category> getCategories() { return categories; }
@Override
public String toString() { return shortName; }
@Override
public String getImageUrl() { return null; }
@Override
public int describeContents() { return 0; }
@Override
public void writeToParcel(android.os.Parcel dest, int flags) {
dest.writeString(departmentId);
dest.writeString(shortName);
dest.writeString(url);
}
}
What is wrong ?
ArrayList
is a subclass of List
, casting from List
to ArrayList
....bad idea...and it will create problems, especially with parcelable stuffs.
So the quick solution is to do:
outState.putParcelableArrayList("key", new ArrayList<Department>(mAdapter.getList()));
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With