I have a RecyclerView with with each item referring to a different
activity. I am able to save the individual items in SharedPreferences but
the reference to the activity is not saved. This is the SharedPreference
class that I created. I want to save not only the individual list in the
recyclerview but also the reference to each activity from their respective
row from the recyclerview
public class SharedPreference {
public static final String PREFS_NAME = "RECIPE_APP";
public static final String FAVORITES = "Recipe_Favorite";
// This four methods are used for maintaining favorites.
public void saveFavorites(Context context, List<ViewRecipe> favorites) {
SharedPreferences settings;
SharedPreferences.Editor editor;
settings = context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
editor = settings.edit();
Gson gson = new Gson();
String jsonFavorites = gson.toJson(favorites);
editor.putString(FAVORITES, jsonFavorites);
editor.commit();
}
public void addFavorite(Context context, ViewRecipe recipe) {
List<ViewRecipe> favorites = getFavorites(context);
if (favorites == null)
favorites = new ArrayList<ViewRecipe>();
favorites.add(recipe);
saveFavorites(context, favorites);
}
public void removeFavorite(Context context, ViewRecipe product) {
ArrayList<ViewRecipe> favorites = (ArrayList<ViewRecipe>)
getFavorites(context);
if (favorites != null) {
favorites.remove(product);
saveFavorites(context, favorites);
}
}
public List<ViewRecipe> getFavorites(Context context) {
SharedPreferences settings;
List<ViewRecipe> favorites;
settings = context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
if (settings.contains(FAVORITES)) {
String jsonFavorites = settings.getString(FAVORITES, null);
Gson gson = new Gson();
ViewRecipe[] favoriteItems = gson.fromJson(jsonFavorites,
ViewRecipe[].class);
favorites = Arrays.asList(favoriteItems);
favorites = new ArrayList<ViewRecipe>(favorites);
} else
return null;
return (ArrayList<ViewRecipe>) favorites;
}
}
How am can I save both the item and the reference to the activity?
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/e03e6ba3-b8e2-475c-b4d5-73b61da6d582%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.