Hi, I am trying to save a row from a RecyclerView into a in a Database. However I ran into some issues and would really appreciate it if you were able to help me. As I said I am saving a row in a RecyclerView with the click of a button. Each row has two TextView and two ImageView. Each row also opens a separate that gives you more details about the row you selected. The row that's saved will appear in a Fragment with a RecyclerView. Currently my app is crashing and I am unable to save the row because I am unable to locate a suitable solution to save the Images in the Database. Thus far I read that I can save the images as a Blob but based on my readings Blob takes up a lot of internal memory. I also read that I can save the Images locally and refer to them via String path but the examples that I have seen is of no help. Hence I am left to reply on the Blob solution but it’s yet to work.
Please see the classes below: ViewRecipe: protected int icon, bookmark; private String title, desp; public ViewRecipe() { super(); } public ViewRecipe(int icon, String title, String desp,int bookmark) { super(); this.setIcon(icon); this.setBookmark(bookmark); this.setTitle(title); this.setDesp(desp); } public int getIcon() { return icon; } public void setIcon(int icon) { this.icon = icon; } public int getBookmark() { return bookmark; } public void setBookmark(int bookmark) { this.bookmark = bookmark; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDesp() { return desp; } public void setDesp(String desp) { this.desp = desp; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + icon; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; return true; } @Override public String toString() { return "ViewRecipe [icon=" + icon + ", bookmark=" + bookmark + ", title=" + title + ", desp=" + desp + "]"; } DBHelper: public static final String DATABASE_NAME = "Recipe.db"; public static final String RECIPE_TABLE = "recipe_table"; public static final String RECIPE_ID = "RECIPE_ID"; public static final String RECIPE_ICON = "RECIPE_ICON"; public static final String RECIPE_TITLE = "RECIPE_TITLE"; public static final String RECIPE_DESCRIPTION = "RECIPE_DESCRIPTION"; public static final String RECIPE_FAVOURITE = "RECIPE_FAVOURITE"; public static final int DB_VERSION = 1; public DBHelper(Context context) { super(context, DATABASE_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("create table " + RECIPE_TABLE + " (RECIPE_ICON BLOB,RECIPE_TITLE TEXT PRIMARY KEY, RECIPE_DESCRIPTION TEXT, RECIPE_FAVOURITE BLOB)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { sqLiteDatabase.execSQL("DROP TABLE IF EXIST " + RECIPE_TABLE); onCreate(sqLiteDatabase); } public void insertData(byte[] icon, String title, String desp, byte[] fav)throws SQLiteException { SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(RECIPE_ICON, icon); contentValues.put(RECIPE_TITLE, title); contentValues.put(RECIPE_DESCRIPTION, desp); contentValues.put(RECIPE_FAVOURITE, fav); db.insert(RECIPE_TABLE, null, contentValues); db.close(); //long results= //if (results == -1) // return false; //else // return true; } public Cursor getAllData(SQLiteDatabase db) { db = this.getReadableDatabase(); Cursor res = db.rawQuery("select * from " + RECIPE_TABLE, null); //byte[] icon =res.getBlob(1); //String title = res.getString(2); //String desp = res.getString(3); //byte[] fav = res.getBlob(4); return res; } RecyclerAdapter: private LayoutInflater mInflater; private List<ViewRecipe> viewRecipes = Collections.emptyList(); private Context mContext; private ClickedListener clickedListener; private LongClickedListener longClickedListener; private DBHelper mDBHelper; public AdapterFavourite(Context context, List<ViewRecipe> data) { this.mContext = context; mInflater = LayoutInflater.from(context); this.viewRecipes = data; mDBHelper = new DBHelper(mContext); } @Override public VHFavourite onCreateViewHolder(ViewGroup parent, int viewType) { View v = mInflater.inflate(R.layout.item_recipe, parent, false); VHFavourite layout = new VHFavourite(v); return layout; } @Override public void onBindViewHolder(VHFavourite holder, int position) { ViewRecipe current = viewRecipes.get(position); Picasso.with(mContext).load(viewRecipes.get(position).getIcon()).resize(80, 80).into(holder.icon); holder.title.setText(current.getTitle()); holder.desp.setText(current.getDesp()); holder.favourite.setImageResource(current.getBookmark()); } public void setClickedListener(ClickedListener clickedListener) { this.clickedListener = clickedListener; } public void setLongClickedListener(LongClickedListener longClickedListener) { this.longClickedListener = longClickedListener; } @Override public int getItemCount() { return viewRecipes.size(); } public class VHFavourite extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { private ImageView icon, favourite; private TextView title, desp; public VHFavourite(View itemView) { super(itemView); icon = (ImageView) itemView.findViewById(R.id.imgRecipeIC); title = (TextView) itemView.findViewById(R.id.txtRecipeTitle); desp = (TextView) itemView.findViewById(R.id.txtRecipeDesp); favourite = (ImageView) itemView.findViewById(R.id.imgFavourite); itemView.setOnClickListener(this); favourite.setOnLongClickListener(this); } @Override public void onClick(View view) { if (clickedListener != null) { clickedListener.itemClicked(view, getPosition()); } } @Override public boolean onLongClick(View view) { if (longClickedListener != null) { longClickedListener.longClicked(view, getPosition()); } // mDBHelper.insertData(icon.getBl.toString(), title.getText().toString(), // desp.getText().toString(), favourite.getDrawable().toString()); boolean isInserted = true; if (isInserted == true) Toast.makeText(mContext, "Favorite Added", Toast.LENGTH_LONG).show(); else Toast.makeText(mContext, "Favorite Removed", Toast.LENGTH_LONG).show(); return false; } } public interface ClickedListener { public void itemClicked(View view, int position); } public interface LongClickedListener { public void longClicked(View view, int position); } Fragment That Shows the Data Saved: public static final String ARG_ITEM_ID = "favorite_list"; private RecyclerView mRecycler; private TextView txtFav; private SharedPreference sharedPreference; private DBHelper mDBHelper; private List<ViewRecipe> favorites; private AdapterRecipe mAdapter; private AdapterFavourite adapter; private LinearLayoutManager mLayout; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View layout = inflater.inflate(R.layout.frag_favourite, container, false); mRecycler = (RecyclerView) layout.findViewById(R.id.recyclerFav); txtFav = (TextView) layout.findViewById(R.id.txtFav); DBFavRecipes(); return layout; } private void DBFavRecipes() { mRecycler.setNestedScrollingEnabled(false); mLayout = new LinearLayoutManager(getContext()); mRecycler.setHasFixedSize(true); mRecycler.setItemAnimator(new DefaultItemAnimator()); mDBHelper = new DBHelper(getActivity()); SQLiteDatabase sqLiteDatabase = mDBHelper.getReadableDatabase(); Cursor cursor = mDBHelper.getAllData(sqLiteDatabase); if (cursor.getCount() == 0) { txtFav.setText(R.string.fav_unavailable); return; } else { if (cursor != null) { if (cursor.moveToFirst()) { do { ViewRecipe recipe = new ViewRecipe(); recipe.setIcon(cursor.getInt(0)); recipe.setTitle(cursor.getString(1)); recipe.setDesp(cursor.getString(2)); recipe.setBookmark(cursor.getInt(3)); favorites.add(recipe); } while (cursor.moveToNext()); mDBHelper.close(); } } } adapter = new AdapterFavourite(getContext(), favorites); mRecycler.setAdapter(adapter); mRecycler.setLayoutManager(mLayout); } The app crashed whenever it reaches recipe.setBookmark(cursor.getInt(3)); in the Fragment Class. I have tried everything and I am unable to solve this issue and hope you can assist me please. Thanks for you help so far. -- 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/6fafc24b-c731-4289-9a7c-db8176868ec3%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.