private ArrayList<PhotoInfo> imagelist;
imagelist = new ArrayList<>();
imagelist = response.body().getPhotos();
I have to save images in shared preference
and retrieve from as Arraylist
private ArrayList<PhotoInfo> imagelist;
imagelist = new ArrayList<>();
imagelist = response.body().getPhotos();
I have to save images in shared preference
and retrieve from as Arraylist
Hey I made this easy methods for Save & Get any custom model's ArrayList into SharedPreferences.
Gson dependency required for this:
implementation 'com.google.code.gson:gson:2.8.5'
Save any custom list into SharedPreferences:
public void saveMyPhotos(ArrayList<PhotoInfo> imagelist ) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
try {
Gson gson = new Gson();
String json = gson.toJson(imagelist);
editor.putString("MyPhotos", json);
editor.commit(); // This line is IMPORTANT !!!
} catch (Exception e) {
e.printStackTrace();
}
}
Get all my saved photos from SharedPreferences:
private ArrayList<PhotoInfo> getAllSavedMyPhotos() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Gson gson = new Gson();
String json = prefs.getString("MyPhotos", null);
Type type = new TypeToken<ArrayList<PhotoInfo>>() {}.getType();
return gson.fromJson(json, type);
}
You can convert your arraylist in string using gson library and store and get in SharedPreferences code in bellow
Add this in gradle dependency
api 'com.google.code.gson:gson:2.8.5'
Convert your list into string and store in SharedPreferences
public static boolean writePhotoInfoJSON(List<PhotoInfo> sb, Context context)
{
try {
SharedPreferences mSettings =
context.getSharedPreferences("yourSharedPreNme", Context.MODE_PRIVATE);
String writeValue = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriSerializer())
.create()
.toJson(sb, new TypeToken<ArrayList<PhotoInfo>>()
{}.getType());
SharedPreferences.Editor mEditor = mSettings.edit();
mEditor.putString("shringName", writeValue);
mEditor.apply();
return true;
} catch(Exception e)
{
return false;
}
}
Get your Custom Array list
public static ArrayList<PhotoInfo> readPhotoInfoJSON(Context context)
{
if(context==null){
return new ArrayList<>();
}
try{
SharedPreferences mSettings =
context.getSharedPreferences("yourSharedPreNme", Context.MODE_PRIVATE);
String loadValue = mSettings.getString("shringName", "");
Type listType = new TypeToken<ArrayList<PhotoInfo>>(){}.getType();
return new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriDeserializer())
.create()
.fromJson(loadValue, listType);
}catch (Exception e){
e.printStackTrace();
}
return new ArrayList<>();
}
public static class UriDeserializer implements JsonDeserializer<Uri> {
@Override
public Uri deserialize(final JsonElement src, final Type srcType,
final JsonDeserializationContext context) throws
JsonParseException {
return Uri.parse(src.toString().replace("\"", ""));
}
}
public static class UriSerializer implements JsonSerializer<Uri> {
public JsonElement serialize(Uri src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(src.toString());
}
}
Firstly, you need to implement Serializable interface in your model class
Next, add implementation 'com.google.code.gson:gson:2.8.2' in your module level build.gradle file
Then, you can save/retrieve your Arraylist to/from shared prefrences in this way :
SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
public List<?> getListObject(String key, Class<?> T) {
Gson gson = new Gson();
ArrayList<String> objStrings = getListString(key);
ArrayList<?> objects = new ArrayList<>();
for (String jObjString : objStrings) {
objects.add(gson.fromJson(jObjString, (Type) T));
}
return objects;
}
private ArrayList<String> getListString(String key) {
return new ArrayList<>(Arrays.asList(TextUtils.split(preferences.getString(key, ""), "‚‗‚")));
}
private void putListString(String key, ArrayList<String> stringList) {
String[] myStringList = stringList.toArray(new String[stringList.size()]);
preferences.edit().putString(key, TextUtils.join("‚‗‚", myStringList)).apply();
}
public void putListObject(String key, List<Category> objArray) {
Gson gson = new Gson();
ArrayList<String> objStrings = new ArrayList<>();
for (Object obj : objArray) {
objStrings.add(gson.toJson(obj));
}
putListString(key, objStrings);
}
Save Arraylist
public static void saveSharedPreferencesLogList(Context context, ArrayList<MenuItemsData> collageList) {
SharedPreferences mPrefs = context.getSharedPreferences("PhotoCollage", context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(collageList);
prefsEditor.putString("myJson", json);
prefsEditor.commit();
}
Get Arraylist
public static ArrayList<MenuItemsData> loadSharedPreferencesLogList(Context context) {
ArrayList<MenuItemsData> savedCollage = new ArrayList<MenuItemsData>();
SharedPreferences mPrefs = context.getSharedPreferences("PhotoCollage", context.MODE_PRIVATE);
Gson gson = new Gson();
String json = mPrefs.getString("myJson", "");
if (json.isEmpty()) {
savedCollage = new ArrayList<MenuItemsData>();
} else {
Type type = new TypeToken<ArrayList<MenuItemsData>>() {
}.getType();
savedCollage = gson.fromJson(json, type);
}
return savedCollage;
}