Reputation: 4109
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.
Upvotes: 1
Views: 560
Reputation: 11
I hav faced this problem earlier... Use generics it will append the value rather than replacing the old datas....
Upvotes: 0
Reputation: 3875
This code may be work for you.
SharedPreferences setting=context.getSharedPreferences("prefName", Activity.MODE_PRIVATE);
SharedPreferences.Editor edit=setting.edit();
edit.putStringSet("myList", new Gson().toJson(arraylist).toString());
edit.commit();
Upvotes: 0
Reputation: 5347
Read myList
from prefName
, append arraylist
to what was already saved in the preferences, write back myList
to preferences.
Upvotes: 1