我正在使用 SharedPreference
在我的项目中。
我知道sharedPreference
可以储存Set<String>
通过使用 prefs.putStringSet()
.但是我有一种情况需要存储 Set<Set<String>>
在 sharedPreference
.
我怎样才能做到这一点?
最佳答案
你可以使用Gson来转换Set<Set<String>>
字符串然后将其保存在 sharepreference
中, 但没有直接的方法将此类型保存在 sharepreference
中
Gson gson = new Gson();
String jsonObject = gson.toJson(your_set_variable);
editor.putString(KEY_PROJECTS, jsonObject );
editor.commit();
已更新: 这是来自谷歌的 Gson 库,如果你需要使用它,将它添加到你的 gradle 依赖项中
compile 'com.google.code.gson:gson:2.8.0'
这个库为您提供了将任何对象转换为字符串,然后将此 json 字符串转换为您想要的类型,stackoverflow 和 google 有很多很多使用它的示例
关于android - 如何将 Set<Set<String>> 存储在 SharedPreferences 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48134723/