private ArrayList<PhotoInfo> imagelist; imagelist = new ArrayList<>(); imagelist = response.body().getPhotos(); I have to save images in shared preferenceand retrieve from as Arraylist
3 Réponses :
Hey, j'ai fabriqué cette méthode facile pour enregistrer et obtenir n'importe quel tableau de modèle personnalisé dans Dépendance GSON requise pour ceci: P> SharedPreferences Code>.
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);
}
@Rajesh content que cela vous aide .. :)
Vous pouvez convertir votre arraylist dans la chaîne à l'aide de la bibliothèque GSON et stockez et entrez dans le code SharedPreferences à ci-dessous
Ajoutez ceci en grade Dépendance p>
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()); } }
Tout d'abord, vous devez implémenter suivant, ajoutez Ensuite, vous pouvez enregistrer / récupérer votre arraylist pour / de préfraisons partagés de cette manière: P> interface Serializable code> dans votre classe de modèle
implémentation 'com.google.code.gson: gson: 2.8.2' code > Dans votre niveau de module
Build.Gradle Code> Fichier P>
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);
}
Ce dont vous avez besoin est une sérialisement des classes de pojo. Utilisez Parcellable ou Serializable ou GSON pour cela. développeur.android.com/reference/android/os/parcelable github.com/google/gson
Pourquoi ne pas les enregistrer dans une base de données locale SQLite? Les solutions fournies sont très inefficaces si vous avez beaucoup de données