[英]How to save Object[] with SharedPreferences?
我尝试了许多解决方案,但没有任何效果。 我不知道这些是错误的,还是我做错了什么。 我有MyClass[] array = new MyClass[] { objects declarations... };
我想用SharedPreferences保存它。 有人可以给我完整的SaveIt() {}
和LoadIt() {}
代码来执行此操作吗?
不可能。
您只能在SharedPrefences SharePreferences.Editor中存储简单值
更新:
如果要使用侧向库,请使用Gson库:
dependencies {
compile 'com.google.code.gson:gson:2.8.2'
}
商店:
Gson gson = new Gson();
//Your json response object value store in json object
JSONObject jsonObject = response.getJSONObject();
//Convert json object to string
String json = gson.toJson(jsonObject);
//Store in the sharedpreference
getPrefs().setUserJson(json);
检索:
String json = getPrefs().getUserJson();
没有构建SharedPreference
来存储复杂的对象,数组或列表。
因此,根据您的情况,我建议使用Realm或SQLite作为本地数据库。 您可以添加,删除或查询项目列表,这更加灵活
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.