存储自定义对象的数组列表
问题描述:
我已经在共享首选项中保存了自定义对象的ArrayList,如下所示:
I have saved ArrayList of custom objects in the shared preferences like this:
SharedPreferences prefs = context.getSharedPreferences("prefName", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putStringSet("myList", new Gson().toJson(arraylist).toString());
editor.apply();
这样做,一旦保存了值,但是当我退出应用程序并重新启动并尝试保存新值时,旧值就消失了.
知道如何保存旧值并附加新值吗?
我想将所有值保存在同一数组中,并保存数组以在每次加载应用程序时显示值.
By doing this, once values are saved but when i exit from the app and relaunch and try to save new values then the old values are gone.
Any idea how can i save old values and append new ones?
I want to keep all values in the same array and keep array saved to show values every time when the app loads.
答
从prefName
读取myList
,将arraylist
附加到首选项中已保存的内容,再将myList
写回首选项.
Read myList
from prefName
, append arraylist
to what was already saved in the preferences, write back myList
to preferences.