我尝试了许多解决方案,但都不起作用。我不知道这些是不是错了,还是我做错了什么。我有MyClass[] array = new MyClass[] { objects declarations... };,我想用SharedPreferences保存它。有人能给我做这件事所需的完整的SaveIt() {}和LoadIt() {}代码吗?
发布于 2018-05-06 00:03:37
不可能。
您只能在SharedPrefences SharePreferences.Editor中存储简单的值
更新:
如果您想使用sideways库,请使用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();发布于 2018-05-06 00:09:10
SharedPreference不是用来存储复杂对象、数组或列表的。
因此,在您的情况下,我建议使用Realm或SQLite作为本地数据库。您可以添加、移除或查询项目列表,这更加灵活
https://stackoverflow.com/questions/50191399
复制相似问题