Android 在listview中显示数据库中的图像
因此,我有一个名为apps的表,其中有4列和一个名为App_icon的列,我将drawable转换为byte[],并将其作为blob存储在列“App_icon”中。我有一个listview,它从表中提取字段,如图所示。我还需要通过将blob转换回图像来显示左角的图像。我用于从数据库检索文本数据并显示的代码是:Android 在listview中显示数据库中的图像,android,database,android-sqlite,Android,Database,Android Sqlite,因此,我有一个名为apps的表,其中有4列和一个名为App_icon的列,我将drawable转换为byte[],并将其作为blob存储在列“App_icon”中。我有一个listview,它从表中提取字段,如图所示。我还需要通过将blob转换回图像来显示左角的图像。我用于从数据库检索文本数据并显示的代码是: mySQLiteAdapter.open(); String[] arrayColumns = new String[] { Dbhelper.KEY_PKG,
mySQLiteAdapter.open();
String[] arrayColumns = new String[] { Dbhelper.KEY_PKG,
Dbhelper.KEY_APP, };
int[] arrayViewIDs = new int[] { R.id.appname, R.id.pkgname };
cursor = mySQLiteAdapter.getAllFolders();
adapter = new SimpleCursorAdapter(
getActivity().getApplicationContext(), R.layout.listview_items,
cursor, arrayColumns, arrayViewIDs);
lv.setAdapter(adapter);
mySQLiteAdapter.close();
其中KEY_PKG和KEY_APP是文本视图中显示的其他表名(appname和pkgname)。有人能提供一个关于如何将图像绑定到相应字段的示例代码吗?
当做
维贾伊
编辑:如果有人可以发布如何从所有行中仅获取“App_icon”列的值并将其存储在数组中?那将是有益的
编辑2:下面是获取图像的方法
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue (View view, Cursor cursor, int columnIndex){
if (view.getId() == R.id.app_icon) {
ImageView IV=(ImageView) view;
Bitmap icon;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
byte[] image = null;
Cursor images = mySQLiteAdapter.geticon();
int rows = images.getCount();
for(int i=1;i<=rows;i++){
image = mySQLiteAdapter.getIcon(i);
icon = BitmapFactory.decodeByteArray(image , 0, image .length, options);
Toast.makeText(getActivity(), "executes"+rows, Toast.LENGTH_SHORT).show();
IV.setImageBitmap(icon);
}
//IV.setBackgroundColor(Color.TRANSPARENT);
return true;
}
return false;
}
});
adapter.setViewBinder(新的SimpleCursorAdapter.ViewBinder(){
@凌驾
公共布尔setViewValue(视图、光标、int-columnIndex){
if(view.getId()==R.id.app_图标){
ImageView IV=(ImageView)视图;
位图图标;
BitmapFactory.Options=new-BitmapFactory.Options();
options.inPreferredConfig=Bitmap.Config.ARGB_8888;
字节[]图像=空;
cursorimages=mySQLiteAdapter.geticon();
int rows=images.getCount();
对于(int i=1;i首先将字节数组转换为位图,然后绑定到视图。因此,自定义适配器可以解决此问题。下面是一个将字节[]转换为位图的简单函数:
public static Bitmap getBitmapFromBytes(byte[] bytes)
{
Bitmap bmp;
bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Bitmap mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
return mutableBitmap;
}
所以您要转换字节[]要位图?@Moh.Sukhni是的…必须将其转换为位图,然后将其添加到列表视图。@Hi-TechKitKatAndroid据我所知,它甚至没有谈论图像。是的,如果我谈论自定义,这意味着它可能有任何东西,甚至列表视图中的列表视图,您可以让您的图像视图将blob更改为位图,并将其设置为simp的imageview你们中有谁能说我如何从所有行中只获取“App_icon”列的值并将其存储在一个数组中?因为我没有sqlite,所以我很困惑。谢谢你们的代码!我将使用它而不是现在的,因为它看起来比我以前的更强大!