1

我在共享首选项中保存了自定义对象的 ArrayList,如下所示:

SharedPreferences prefs = context.getSharedPreferences("prefName", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putStringSet("myList", new Gson().toJson(arraylist).toString());
editor.apply();  

通过这样做,一旦保存了值,但是当我退出应用程序并重新启动并尝试保存新值时,旧值就消失了。
知道如何保存旧值并附加新值吗?
我想将所有值保存在同一个数组中,并保存数组以在每次加载应用程序时显示值。

4

3 回答 3

1

myList从读取prefName,附加arraylist到已保存在首选项中的内容,写回首myList选项。

于 2013-03-06T08:47:27.273 回答
0

我之前遇到过这个问题......使用泛型它将附加值而不是替换旧数据......

于 2013-03-06T12:20:15.450 回答
0

此代码可能对您有用。

 SharedPreferences setting=context.getSharedPreferences("prefName", Activity.MODE_PRIVATE);
 SharedPreferences.Editor edit=setting.edit();
 edit.putStringSet("myList", new Gson().toJson(arraylist).toString());
 edit.commit();
于 2013-03-06T09:10:59.990 回答