/**
* Save and get ArrayList in SharedPreference
*/
public void saveArrayList(ArrayList<String> list, String key){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
SharedPreferences.Editor editor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(list);
editor.putString(key, json);
editor.apply(); // This line is IMPORTANT !!!
}
public ArrayList<String> getArrayList(String key){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
Gson gson = new Gson();
String json = prefs.getString(key, null);
Type type = new TypeToken<ArrayList<String>>() {}.getType();
return gson.fromJson(json, type);
}
Last active
July 13, 2021 18:40
-
-
Save WrathChaos/5f39e3ce3874a049d25e2ca8958d18b6 to your computer and use it in GitHub Desktop.
How to save and get ArrayList in SharedPreference on Android? Article: https://freakycoder.com/android-notes-40-how-to-save-and-get-arraylist-into-sharedpreference-7d1f044bc79a
Thanks for the share. but how to delete saved list?. editor.remove(key) not worked
try editor.remove(key).apply()
great work there
How to display that saved array in list view??
How to display that saved array in list view??
You need to google it for that :) Not the right place to ask :)
How to work with ArrayList ?
Note that CustomClass has Date, Int and String fields
Hey @oremlawi,
I really could not understand your question. Can you explain it a bit?
Am using it on a fragment page... But the data is not loading back.
Phenomenal !! U saved me
Love to hear that @thanhdat410 :)
I suggest you to write a simple util class to use them a much better way.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the share. but how to delete saved list?. editor.remove(key) not worked