我试图在从sqlite数据库中获取数据后动态显示Android Gridview
中的图像。我尝试了ImageAdapter
个例子,但我没有成功。任何人都可以帮助我,如何获取数据并将其传递给ImageAdapter.getView()
方法,以便它显示图像?
以下是我的来源
1.从db
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;
}
}
答案 0 :(得分:0)
在android中显示列表或网格中带有图像的项目就像这样(示例):
以下是APhotoManager的工作示例。它包含带有gridview的GalleryCursorFragment和带有嵌入式GridCellViewHolder的GalleryCursorFragment