【发布时间】:2013-11-20 06:41:41
【问题描述】:
我正在保存我的
"ArrayList<Item> items = new ArrayList<Item>();"
在 Shared Preference 中使用 GSON,下面的代码运行良好
protected static void saveItems(){
SharedPreferences prefs = context.getSharedPreferences("prefName", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("myList", new Gson().toJson(items).toString());
editor.apply();
}
检索
protected void retreiveItems(){
preferences = context.getSharedPreferences("prefName",android.content.Context.MODE_PRIVATE);
saveitems = preferences.getString("myList", "");
Log.d("LOG", "Retreived Items : " + saveitems);
}
但检索再次给出 JSON 格式。
Retreived Items : [{"subtitle":"20 Nov 2013 12:35:19","title":"Sync Successful"},{"subtitle":"20 Nov 2013 12:35:44","title":"Sync Successful"}]
如何将每个 JSON 集提取到两个不同的字符串中,以便将其添加到我的列表视图中
items.add(new EntryItem(title, subtitle));
【问题讨论】:
-
您应该首先检查您在首选项中输入的值。 new Gson().toJson(items).toString()->它给了你什么?
-
How can i extract each JSON set into two different strings so that i can add it into my listview.除了将其保存为两个不同的字符串之外,最好对其进行解析并将其存储到创建 POJO 类的自定义数组列表中。
标签: android listview sharedpreferences gson