- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我不想重新加载RecyclerView
屏幕旋转后。我发现我需要存储/恢复 List
来自 Adapter
.不是吗?
但是有问题:
Found java.util.List<Department> requried java.util.ArrayList<?extends android.os.Parcelable>
当我尝试将列表放入包中时:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("key", mAdapter.getList());
}
mAdapter.getList()
返回 List<Department>
来自 RecyclerView 的适配器。
但我已经实现了Parcelable
里面Department
模型类:
@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);
}
}
怎么了?
最佳答案
ArrayList
是 List
的子类, 从 List
类型转换至 ArrayList
....坏主意...它会产生问题,尤其是对于可打包的东西。
所以快速的解决方案是:
outState.putParcelableArrayList("key", new ArrayList<Department>(mAdapter.getList()));
.
关于android - 在 onSaveInstanceState 中保存对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37966608/
我是 Android 开发新手。我正在使用 onSaveInstanceState() 的覆盖版本来保存我自己的应用程序数据。我注意到我没有在我的函数中调用 super.onSaveInstanceS
如果我将在不调用 super.onSaveInstanceState(outState) 的情况下使用此代码; @Override protected void onSaveInstanceState
我在 fragment 兼容性包示例之后创建了一个 fragment 选项卡小部件,我在 onTabChanged 中更改了 fragment ,但我有一些市场报告在 onSaveInstanceSt
当我离开我的 Activity 时,为什么我的应用程序崩溃了?更具体地说:在我实现 onSaveInstanceState() 之前一切正常和 onRestoreInstanceState() 这是代
我有一个 Activity ,它启动结果代码的各种 Activity ,并在 onActivityResult 方法中获取结果时,它根据结果代码启动适当的 Activity 。 onSaveInsta
我在 onSaveInstanceState 上保存当前进度条时遇到问题。我保存当前正在进行的时间,但是当退出 Activity 并返回时恢复进度不显示剩余时间。 该类显示进度条 public cla
我想实现什么目标? 我想保存 x、y 和 score 的值,因此当用户旋转屏幕时,会检索这些值,而不是重获新生。 我有什么? 可能是正确的 onSaveInstanceState 代码,但由于某些 @
所以我了解了如何使用的主要想法 protected void onSaveInstanceState (Bundle outState) http://developer.android.com/
考虑以下代码,它将静态变量:mMyArray 保存在 onSaveInstanceState 中,并在 onRestoreInstanceState 中恢复它,但是我发现它根本无法保存 int 数组。
当我的应用程序变得复杂并且主要 Activity 开始在内存中保存大量数据(响应用户操作从文件加载和解析)时,我(错误地)从 onSaveInstanceState 切换到 onRetainNonCo
我是我应用的 onSaveInstanceState 实现。我已经成功实现了我的 onSaveInstanceState,但它不会在方向更改时保持当前问题。以下是我的 MainActivity。我一直
我正在尝试在单击后退按钮时保存和检索我的 Activity 的 onSaveInstanceState()。我读过默认情况下它不会保存,因为假设用户在单击返回时已完成 Activity 。但是,在我的
我已经阅读了android文档中的以下声明 Because onSaveInstanceState() is not guaranteed to be called, you should use i
我有几个扩展 LinearLayout 的自定义组件。 现在我想在屏幕方向改变时保存/恢复它们的状态,但我不能通过 Activity 的 onSaveInstanceState/onRestoreIn
我正在尝试了解生命周期方法和 onSaveInstanceState()。 我实际上只需要担心 onCreate() 和 onPause() 是否正确? onCreate()? 我可以/应该忽略 on
我是 Android 开发的新手,我有一个关于 onSaveInstanceState() 的问题。我目前正在处理一个应用程序的登录 Activity 。为了检查用户是否可以登录他们的帐户,我对服务器
我正在编写一个程序,我在同一个 Activity 中调用多个布局,但后来我注意到当我切换布局时,切换之前所做的更改不会恢复并且 onSavedInstanceState(Bundle outState
当 android 由于系统限制而杀死您的进程时,它使您能够通过将数据存储在一个包中来跨进程持久保存数据。如果您的进程被终止,这个包保存在哪里?它存在于哪个进程中?它住在内存中的什么地方?它存在于内核
我在我的 android Activity 中实现了 onSaveInstanceState 和 onRestoreInstanceState。 这些似乎工作正常,但我想在某些事情发生时(例如,当用户
我的问题似乎与其他人相反。 :) 每当我从一个 Activity 导航到下一个 Activity 时,都会调用我的 onSaveInstanceState。我检查了 LogCat,它绝对不会终止 Ac
我是一名优秀的程序员,十分优秀!