how to store and retrieve images in android SQLite database and display them on a gridview

前端 未结 2 1289
情话喂你
情话喂你 2020-12-28 09:29

I am new to android. I created a table that has an itemName,Price and image. I am trying to retrieve the image and name fields and display them on a gridview

Here is

2条回答
  •  自闭症患者
    2020-12-28 09:53

    It depends on how you want to store your Image, because you are storing as a byte array here is how to retrieve it

      public Cursor fetchAll() {
                return databaseHelper.query(ITEMS_TABLE, new String[] { COLUMN_ITEM_ID,   COLUMN_ITEM_NAME, COLUMN_ITEM_SPECS,COLUMN_ITEM_PRICE, COLUMN_ITEM_IMAGE,COLUMN_ITEM_QTY},null, null, null, null, null);
            }
    /*    this method will give you a cursor with all the records of your database table now you need to parse these records on to objects*/
    
    private List parse(Cursor cursor) {
         List toReturn = null;
         toReturn = new ArrayList();
         ItemsPojo obj;
         if (cursor.moveToFirst()) {
              while (cursor.isAfterLast() == false) {
                   obj = new ItemsPojo();
                   obj.setId(cursor.getInt(0));
                   obj.setItemName(cursor.getString(1));
                   obj.setItemSpecs(cursor.getString(2));
                   obj.setItemPrice(cursor.getDouble(3));
                   obj.setItemImg(cursor.getBlob(4));
                   obj.setItemQuantity(cursor.getInt(5));
                   toReturn.add(obj);
                   cursor.moveToNext();
                   }
         }return toReturn;
    }
    

    now you have a List with all your records on your database, now what you need to do is create a ListActivity and show all your objects on it, If you dont know how to do this please post your comment here or if you have any doubt about the procedure above

提交回复
热议问题