我是安卓新手。我创建了一个包含 itemName、Price 和 image 的表。我正在尝试检索图像和名称字段并将它们显示在网格视图上
这是我的创建数据库语句:
DBAdapter class onCreate()
db.execSQL("CREATE TABLE "+ITEMS_TABLE+" ( "+ COLUMN_ITEM_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_ITEM_NAME +" TEXT, "+ COLUMN_ITEM_SPECS +" TEXT, " + COLUMN_ITEM_PRICE +
" NUMBER, " + COLUMN_ITEM_IMAGE + " BLOB, " + COLUMN_ITEM_QTY +" NUMBER)");
void AddItems(ItemsPojo itemsObj)
{
Log.i("in here", "item fields");
SQLiteDatabase db= DBHelper.getWritableDatabase();
if (db==null)
{
Log.i("nulll", "mnllllsg");
}
ContentValues cv=new ContentValues();
cv.put(COLUMN_ITEM_NAME, itemsObj.getItemName());
cv.put(COLUMN_ITEM_SPECS, itemsObj.getItemSpecs());
cv.put(COLUMN_ITEM_PRICE, itemsObj.getItemPrice());
cv.put(COLUMN_ITEM_QTY, itemsObj.getItemQty());
cv.put(COLUMN_ITEM_IMAGE, itemsObj.getItemImg());
long affectedColumnId = db.insert(ITEMS_TABLE, null, cv);
db.close();
}
public Bitmap getAllImages()
{
SQLiteDatabase db=DBHelper.getWritableDatabase();
//Cursor cur= db.rawQuery("Select "+colID+" as _id , "+colName+", "+colAge+" from "+employeeTable, new String [] {});
Cursor cur= db.rawQuery("SELECT * FROM "+ITEMS_TABLE,null);
//if(cur.getCount() > 0){
//Cursor c = mDb.query(" ... ");
cur.moveToFirst();
ByteArrayInputStream inputStream = new ByteArrayInputStream(cur.getBlob(cur.getColumnIndex(COLUMN_ITEM_IMAGE)));
Bitmap b = BitmapFactory.decodeStream(inputStream);
// }
return b;
}
在我的 Main Oncreate 中,我像这样填充我的数据库:
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.red_bn);
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, out);
byte[] b = out.toByteArray();
String name;
name="QUEEN_BED1";
String specs = "blaBlaBla";
double price = 5420;
int qty = 10;
ItemsPojo itemsObj = new ItemsPojo(name,specs,price,b,qty);
db.AddItems(itemsObj);
我现在卡住了,请谁能帮我检索这张图片并将其显示在网格视图上?