REF _ https://codechacha.com/ko/sharedpref_arraylist/
public class SavedContactList {
public static void setStringArrayPref(Context context, List<String> values) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
JSONArray a = new JSONArray();
for (int i = 0; i < values.size(); i++) {
a.put(values.get(i));
}
if (!values.isEmpty()) {
editor.putString("savedContactList", a.toString());
} else {
editor.putString("savedContactList", null);
}
editor.apply();
}
public static List<String> getStringArrayPref(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String json = prefs.getString("savedContactList", null);
List<String> urls = new ArrayList<>();
if (json != null) {
try {
JSONArray a = new JSONArray(json);
for (int i = 0; i < a.length(); i++) {
String url = a.optString(i);
urls.add(url);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return urls;
}
public static void setArrayNull(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("savedContactList", null);
editor.apply();
}
}