0
我想在從sqlite數據庫獲取數據後動態顯示Android Gridview
中的圖像。我嘗試了ImageAdapter
的例子,但我沒有成功。任何人都可以幫助我,如何獲取數據並將其傳遞給ImageAdapter.getView()
方法,以便它能顯示圖像? 下面是我的源從分貝在從數據庫中提取圖像後在Android GridView中顯示圖像
private void fun_FecthData() {
Cursor cur = db.query(false, "goodsmgnt1", new String[]{ "itemimage"}, null,null,
null, null, null, null);
//Toast.makeText(mContext.getApplicationContext(), "No of Image = " + cur.getCount(), Toast.LENGTH_LONG).show();
if (cur.getCount() < 1) {
cur.close();
}
cur.moveToFirst();
for (int i = 0; i < cur.getCount(); i++) {
byte[] itemimgblob = cur.getBlob(cur.getColumnIndex("itemimage"));
Bitmap itemImage = BitmapFactory.decodeByteArray(itemimgblob, 0, itemimgblob.length);
img_bitmap.add(itemImage);//Image stored in Bitmap array List
cur.moveToNext();
}
db.close();
ImageAdapter Getview()
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it’s not recycled, initialize some attributes
Toast.makeText(mContext.getApplicationContext(), "Getview if " , Toast.LENGTH_LONG).show();
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
Toast.makeText(mContext.getApplicationContext(), "Getview else " , Toast.LENGTH_LONG).show();
imageView = (ImageView) convertView;
}
File imgFile = new File(img_bitmap.get(position).toString());
if(imgFile.exists()){
Toast.makeText(mContext.getApplicationContext(), "Getview Position = " + position, Toast.LENGTH_LONG).show();
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
}
return imageView;
}
}
該代碼向我們展示瞭如何下載圖像。你是否也有適配器的代碼?你可以給我們一個html-link到'ImageAdapter example'嗎? – k3b