Re: [android-developers] Save RecyclerView Item with Activity reference in SharedPreferences

2017-01-02 Thread toneymessina via Android Developers


On Tue, 1/3/17, lekishahendrix via Android Developers 
<android-developers@googlegroups.com> wrote:

 Subject: Re: [android-developers] Save RecyclerView Item with Activity 
reference in SharedPreferences
 To: android-developers@googlegroups.com
 Date: Tuesday, January 3, 2017, 9:02 AM
 
 
 
 On Tue, 1/3/17, Martiman Henry <martimanhe...@gmail.com>
 wrote:
 
  Subject: Re: [android-developers] Save RecyclerView Item
 with Activity reference in SharedPreferences
  To: android-developers@googlegroups.com
  Date: Tuesday, January 3, 2017, 5:34 AM
  
  
  
  Hi
  Marina, hope all is well. I made some
  adjustments to my code and would really appreciate it if
 you
  were able to help
  me implement the Database Helper.
  
   
  
   
  
   
  
  View
  Class:
  
  public
  class ViewDB {
  
   
  
      private Bitmap icon,
  fav;
  
      private String name,
  desp;
  
      private int
  icRecipe;
  
      private boolean
  cbFav;
  
   
  
      public ViewDB()
  {
  
     
  super();
  
      }
  
   
  
      public ViewDB(int
  icRecipe, String name,
  String desp, boolean cbFav) {
  
     
  super();
  
     
  this.setIcRecipe(icRecipe);
  
     
  this.setName(name);
  
     
  this.setDesp(desp);
  
     
  this.setCbFav(cbFav);
  
      }
  
   
  
      public ViewDB(Bitmap
  icon, String name,
  String desp) {
  
      icon =
  icon;
  
      name =
  name;
  
      desp =
  desp;
  
      }
  
   
  
      public Bitmap
  getIcon() {
  
      return
  icon;
  
      }
  
   
  
      public void
  setIcon(Bitmap icon) {
  
      this.icon =
  icon;
  
      }
  
   
  
      public Bitmap
  getFav() {
  
      return
  fav;
  
      }
  
   
  
      public void
  setFav(Bitmap fav) {
  
      this.fav =
  fav;
  
      }
  
   
  
      public String
  getName() {
  
      return
  name;
  
      }
  
   
  
      public void
  setName(String name) {
  
      this.name = name;
  
      }
  
   
  
      public String
  getDesp() {
  
     
  return desp;
  
      }
  
   
  
      public void
  setDesp(String desp) {
  
      this.desp =
  desp;
  
      }
  
   
  
      public int
  getIcRecipe() {
  
      return
  icRecipe;
  
      }
  
   
  
      public void
  setIcRecipe(int icRecipe) {
  
     
  this.icRecipe = icRecipe;
  
      }
  
   
  
      public boolean
  isCbFav() {
  
      return
  cbFav;
  
      }
  
   
  
      public void
  setCbFav(boolean cbFav) {
  
      this.cbFav =
  cbFav;
  
      }
  
  }
  
   
  
   
  
   
  
   
  
  Database Helper class:
  
  public
  class
  DBHelper  extends SQLiteOpenHelper
  {
  
   
  
      public static final
  String DB_NAME =
  "Recipes.db";
  
      private static final
  String DB_TABLE =
  "recipe_table";
  
      private static final
  int DB_VERSION = 1;
  
      private static final
  String RECIPE_ID =
  "ID";
  
      private static final
  String RECIPE_ICON =
  "ICON";
  
      private static final
  String RECIPE_NAME =
  "NAME";
  
      private static final
  String RECIPE_DESP =
  "DESCRIPTION";
  
      private static final
  String RECIPE_FAV =
  "FAVOURITE";
  
      private static final
  String
  CREATE_RECIPE_TABLE = "create table "
  
      +
  DB_NAME + " (" +
  RECIPE_ID + "integer primary key autoincrement,
  "
  
      +
  RECIPE_ICON + " blob not
  null, " + RECIPE_NAME + " text not null unique,
  "
  
      +
  RECIPE_DESP + " text not
  null);";
  
      public static final
  Object[] databaseLock =
  new Object[0];
  
      private Context
  mContext;
  
   
  
      public
  DBHelper(Context context) {
  
     
  super(context, DB_NAME, null,
  DB_VERSION);
  
      }
  
   
  
      @Override
  
      public void
  onCreate(SQLiteDatabase
  sqLiteDatabase) {
  
     
  sqLiteDatabase.execSQL(CREATE_RECIPE_TABLE);
  
      }
  
   
  
      @Override
  
      public void
  onUpgrade(SQLiteDatabase
  sqLiteDatabase, int i, int i1) {
  
     
  sqLiteDatabase.execSQL("DROP TABLE
  IF EXIST " + DB_TABLE);
  
     
  onCreate(sqLiteDatabase);
  
      }
  
   
  
      //Add
  Favourite
  
      public void
  addFav(ViewDB viewDB) throws
  SQLiteException {
  
   
  
    
   synchronized (databaseLock)
  {
  
     
  SQLiteDatabase db =
  this.getWritableDatabase();
  
      if
  (db != null) {
  
     
  ContentValues contentValues =
  new ContentValues();
  
     
  contentValues.put(RECIPE_ICON,
  DBBitmapUtility.getBytes(viewDB.getIcon()));
  
     
  contentValues.put(RECIPE_NAME,
  viewDB.getName());
  
     
  contentValues.put(RECIPE_DESP,
  viewDB.getDesp())

Re: [android-developers] Save RecyclerView Item with Activity reference in SharedPreferences

2017-01-02 Thread lekishahendrix via Android Developers


On Tue, 1/3/17, Martiman Henry <martimanhe...@gmail.com> wrote:

 Subject: Re: [android-developers] Save RecyclerView Item with Activity 
reference in SharedPreferences
 To: android-developers@googlegroups.com
 Date: Tuesday, January 3, 2017, 5:34 AM
 
 
 
 Hi
 Marina, hope all is well. I made some
 adjustments to my code and would really appreciate it if you
 were able to help
 me implement the Database Helper.
 
  
 
  
 
  
 
 View
 Class:
 
 public
 class ViewDB {
 
  
 
     private Bitmap icon,
 fav;
 
     private String name,
 desp;
 
     private int
 icRecipe;
 
     private boolean
 cbFav;
 
  
 
     public ViewDB()
 {
 
    
 super();
 
     }
 
  
 
     public ViewDB(int
 icRecipe, String name,
 String desp, boolean cbFav) {
 
    
 super();
 
    
 this.setIcRecipe(icRecipe);
 
    
 this.setName(name);
 
    
 this.setDesp(desp);
 
    
 this.setCbFav(cbFav);
 
     }
 
  
 
     public ViewDB(Bitmap
 icon, String name,
 String desp) {
 
     icon =
 icon;
 
     name =
 name;
 
     desp =
 desp;
 
     }
 
  
 
     public Bitmap
 getIcon() {
 
     return
 icon;
 
     }
 
  
 
     public void
 setIcon(Bitmap icon) {
 
     this.icon =
 icon;
 
     }
 
  
 
     public Bitmap
 getFav() {
 
     return
 fav;
 
     }
 
  
 
     public void
 setFav(Bitmap fav) {
 
     this.fav =
 fav;
 
     }
 
  
 
     public String
 getName() {
 
     return
 name;
 
     }
 
  
 
     public void
 setName(String name) {
 
     this.name = name;
 
     }
 
  
 
     public String
 getDesp() {
 
    
 return desp;
 
     }
 
  
 
     public void
 setDesp(String desp) {
 
     this.desp =
 desp;
 
     }
 
  
 
     public int
 getIcRecipe() {
 
     return
 icRecipe;
 
     }
 
  
 
     public void
 setIcRecipe(int icRecipe) {
 
    
 this.icRecipe = icRecipe;
 
     }
 
  
 
     public boolean
 isCbFav() {
 
     return
 cbFav;
 
     }
 
  
 
     public void
 setCbFav(boolean cbFav) {
 
     this.cbFav =
 cbFav;
 
     }
 
 }
 
  
 
  
 
  
 
  
 
 Database Helper class:
 
 public
 class
 DBHelper  extends SQLiteOpenHelper
 {
 
  
 
     public static final
 String DB_NAME =
 "Recipes.db";
 
     private static final
 String DB_TABLE =
 "recipe_table";
 
     private static final
 int DB_VERSION = 1;
 
     private static final
 String RECIPE_ID =
 "ID";
 
     private static final
 String RECIPE_ICON =
 "ICON";
 
     private static final
 String RECIPE_NAME =
 "NAME";
 
     private static final
 String RECIPE_DESP =
 "DESCRIPTION";
 
     private static final
 String RECIPE_FAV =
 "FAVOURITE";
 
     private static final
 String
 CREATE_RECIPE_TABLE = "create table "
 
     +
 DB_NAME + " (" +
 RECIPE_ID + "integer primary key autoincrement,
 "
 
     +
 RECIPE_ICON + " blob not
 null, " + RECIPE_NAME + " text not null unique,
 "
 
     +
 RECIPE_DESP + " text not
 null);";
 
     public static final
 Object[] databaseLock =
 new Object[0];
 
     private Context
 mContext;
 
  
 
     public
 DBHelper(Context context) {
 
    
 super(context, DB_NAME, null,
 DB_VERSION);
 
     }
 
  
 
     @Override
 
     public void
 onCreate(SQLiteDatabase
 sqLiteDatabase) {
 
    
 sqLiteDatabase.execSQL(CREATE_RECIPE_TABLE);
 
     }
 
  
 
     @Override
 
     public void
 onUpgrade(SQLiteDatabase
 sqLiteDatabase, int i, int i1) {
 
    
 sqLiteDatabase.execSQL("DROP TABLE
 IF EXIST " + DB_TABLE);
 
    
 onCreate(sqLiteDatabase);
 
     }
 
  
 
     //Add
 Favourite
 
     public void
 addFav(ViewDB viewDB) throws
 SQLiteException {
 
  
 
   
  synchronized (databaseLock)
 {
 
    
 SQLiteDatabase db =
 this.getWritableDatabase();
 
     if
 (db != null) {
 
    
 ContentValues contentValues =
 new ContentValues();
 
    
 contentValues.put(RECIPE_ICON,
 DBBitmapUtility.getBytes(viewDB.getIcon()));
 
    
 contentValues.put(RECIPE_NAME,
 viewDB.getName());
 
    
 contentValues.put(RECIPE_DESP,
 viewDB.getDesp());
 
  
 
    
 try {
 
    
 db.insert(DB_TABLE, null,
 contentValues);
 
    
 } catch (Exception e) {
 
    
 Toast.makeText(mContext,
 "Unable to add favourite.",
 Toast.LENGTH_LONG).show();
 
    
 }
 
    
 db.close();
 
    
 }
 
     }
 
     }
 
  
 
     //Open
 Database
 
     public DBHelper
 open() throws SQLException
 {
 
    
 this.getWritableDatabase();
 
     return
 this;
 
     }
 
  
 
     // Close
 Database
 
     public void close()
 {
 
    
 this.close();
 
     }
 
  
 
     //Delete
 Favourite
 
     public Integer
 deleteFav(String id) {
 
    
 SQLiteDatabase db

Re: [android-developers] Save RecyclerView Item with Activity reference in SharedPreferences

2017-01-02 Thread Martiman Henry
Hi Marina, hope all is well. I made some adjustments to my code and would
really appreciate it if you were able to help me implement the Database
Helper.







View Class:

public class ViewDB {



private Bitmap icon, fav;

private String name, desp;

private int icRecipe;

private boolean cbFav;



public ViewDB() {

super();

}



public ViewDB(int icRecipe, String name, String desp, boolean cbFav) {

super();

this.setIcRecipe(icRecipe);

this.setName(name);

this.setDesp(desp);

this.setCbFav(cbFav);

}



public ViewDB(Bitmap icon, String name, String desp) {

icon = icon;

name = name;

desp = desp;

}



public Bitmap getIcon() {

return icon;

}



public void setIcon(Bitmap icon) {

this.icon = icon;

}



public Bitmap getFav() {

return fav;

}



public void setFav(Bitmap fav) {

this.fav = fav;

}



public String getName() {

return name;

}



public void setName(String name) {

this.name = name;

}



public String getDesp() {

return desp;

}



public void setDesp(String desp) {

this.desp = desp;

}



public int getIcRecipe() {

return icRecipe;

}



public void setIcRecipe(int icRecipe) {

this.icRecipe = icRecipe;

}



public boolean isCbFav() {

return cbFav;

}



public void setCbFav(boolean cbFav) {

this.cbFav = cbFav;

}

}









Database Helper class:

public class DBHelper  extends SQLiteOpenHelper {



public static final String DB_NAME = "Recipes.db";

private static final String DB_TABLE = "recipe_table";

private static final int DB_VERSION = 1;

private static final String RECIPE_ID = "ID";

private static final String RECIPE_ICON = "ICON";

private static final String RECIPE_NAME = "NAME";

private static final String RECIPE_DESP = "DESCRIPTION";

private static final String RECIPE_FAV = "FAVOURITE";

private static final String CREATE_RECIPE_TABLE = "create table "

+ DB_NAME + " (" + RECIPE_ID + "integer primary key
autoincrement, "

+ RECIPE_ICON + " blob not null, " + RECIPE_NAME + " text not
null unique, "

+ RECIPE_DESP + " text not null);";

public static final Object[] databaseLock = new Object[0];

private Context mContext;



public DBHelper(Context context) {

super(context, DB_NAME, null, DB_VERSION);

}



@Override

public void onCreate(SQLiteDatabase sqLiteDatabase) {

sqLiteDatabase.execSQL(CREATE_RECIPE_TABLE);

}



@Override

public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

sqLiteDatabase.execSQL("DROP TABLE IF EXIST " + DB_TABLE);

onCreate(sqLiteDatabase);

}



//Add Favourite

public void addFav(ViewDB viewDB) throws SQLiteException {



synchronized (databaseLock) {

SQLiteDatabase db = this.getWritableDatabase();

if (db != null) {

ContentValues contentValues = new ContentValues();

contentValues.put(RECIPE_ICON,
DBBitmapUtility.getBytes(viewDB.getIcon()));

contentValues.put(RECIPE_NAME, viewDB.getName());

contentValues.put(RECIPE_DESP, viewDB.getDesp());



try {

db.insert(DB_TABLE, null, contentValues);

} catch (Exception e) {

Toast.makeText(mContext, "Unable to add favourite.",
Toast.LENGTH_LONG).show();

}

db.close();

}

}

}



//Open Database

public DBHelper open() throws SQLException {

this.getWritableDatabase();

return this;

}



// Close Database

public void close() {

this.close();

}



//Delete Favourite

public Integer deleteFav(String id) {

SQLiteDatabase db = this.getWritableDatabase();

return db.delete(DB_TABLE, "ID=?", new String[]{id});

}



//Show All Favourites

public ViewDB getAllFav() throws SQLException {

SQLiteDatabase db = this.getWritableDatabase();

Cursor cur = db.query(true, DB_TABLE, new String[]{RECIPE_ICON,
RECIPE_NAME,

RECIPE_DESP, RECIPE_FAV}, null, null, null, null, null,
null);



if (cur.moveToFirst()) {

byte[] icon = cur.getBlob(cur.getColumnIndex(RECIPE_ICON));

String name = cur.getString(cur.getColumnIndex(RECIPE_NAME));

String desp = cur.getString(cur.getColumnIndex(RECIPE_DESP));

cur.close();

return new ViewDB(DBBitmapUtility.getImage(icon), name, desp);

}

cur.close();

return null;

}



//Auto-refresh Favourite





}







Bitmap Converter class:

public class 

Re: [android-developers] Save RecyclerView Item with Activity reference in SharedPreferences

2016-12-21 Thread Marina Cuello
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 
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 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 favorites = getFavorites(context);
> if (favorites == null)
> favorites = new ArrayList();
> favorites.add(recipe);
> saveFavorites(context, favorites);
> }
>
> public void removeFavorite(Context context, ViewRecipe product) {
> ArrayList favorites = (ArrayList)
> getFavorites(context);
> if (favorites != null) {
> favorites.remove(product);
> saveFavorites(context, favorites);
> }
> }
>
> public List getFavorites(Context context) {
> SharedPreferences settings;
> List 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(favorites);
> } else
> return null;
>
> return (ArrayList) 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 android-developers+unsubscr...@googlegroups.com.
> To post to this group, send email to android-developers@googlegroups.com.
> 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.
>

-- 
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 android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
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.


[android-developers] Save RecyclerView Item with Activity reference in SharedPreferences

2016-12-20 Thread Martiman Henry
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 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 favorites = getFavorites(context);
if (favorites == null)
favorites = new ArrayList();
favorites.add(recipe);
saveFavorites(context, favorites);
}

public void removeFavorite(Context context, ViewRecipe product) {
ArrayList favorites = (ArrayList) 
getFavorites(context);
if (favorites != null) {
favorites.remove(product);
saveFavorites(context, favorites);
}
}

public List getFavorites(Context context) {
SharedPreferences settings;
List 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(favorites);
} else
return null;

return (ArrayList) 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 android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
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.