I get it that you have a reference to the activities in each ViewRecipe and
you're expecting the Gson instance to include it on your file.

If that's the case, you need you find another way: When you take an object
and turn it to json using Gson, you only get the items that are
Serializable. An Activity doesn't implement that interface.

If you just need it to start a different activity when an item is clicked,
you could use an Enum or simply an integer as reference and use a switch to
decide which XXActivity.class to use.


Marina


On Tue, Dec 20, 2016 at 11:33 AM, Martiman Henry <[email protected]>
wrote:

> 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
> <https://groups.google.com/d/msgid/android-developers/e03e6ba3-b8e2-475c-b4d5-73b61da6d582%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CACaNmX388q1pVyhV1L-Fz3JZ41SFhFKgZ5jzXBwHBeXnDT-vtw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to