I have a CustomClass
which is Bean/Model/Pojo
and it is used to save Custom Obejcts from Json. I want to save the ArrayList
of my CustomClass
in SharedPreference
and retrieve it back in a special scenario. I found a way of storing it in SharedPreference
here! The correct answer is working by SpyZip
I am able to store and retrieve a List
but I want to store and retrieve an ArrayList
instead.
Here is a snippet showing how I store and retrieve value in SharedPreference
:
// This method will save custom class ArrayList<Bean>
public void saveUserFavouriteStations(
ArrayList<RadioStationBean> radioStation) {
FavouriteStationHolder = this.getSharedPreferences("stations", 0);
Editor editor = FavouriteStationHolder.edit();
Gson gson = new Gson();
String jsonCars = gson.toJson(radioStation);
editor.putString("stations", jsonCars);
System.out.println("Custom ArrayList Saved in App Class");
editor.commit();
}
public ArrayList<RadioStationBean> getUserFavouriteStations() {
FavouriteStationHolder = this.getSharedPreferences("stations", 0);
if (FavouriteStationHolder != null) {
String jsonString = FavouriteStationHolder
.getString("stations", "");
Type type = new TypeToken<List<RadioStationBean>>() {
}.getType();
List<RadioStationBean> carsList = gson.fromJson(jsonString, type);
return carsList;
} else {
return null;
}
}