Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从本地数据库检索图像并在ListView(Android)中显示_Android_Listview - Fatal编程技术网

如何从本地数据库检索图像并在ListView(Android)中显示

如何从本地数据库检索图像并在ListView(Android)中显示,android,listview,Android,Listview,[]我有一个使用SQLite Manager创建的本地数据库,我想做的是从数据库中检索数据(项目名称、价格和图像),并显示在列表视图中。我将图像存储在drawable文件夹(kids.jpg)中,并将图片的名称存储为字符串(kids) listview的内容将根据查询条件不断变化 我现在面临的问题是,我可以检索图像的名称、价格甚至名称,并在listview中列出它,但我不知道如何在listview中显示图像 我知道下面的代码可以用来从字符串中调用drawable image,但是如何将代码插入到

[]我有一个使用SQLite Manager创建的本地数据库,我想做的是从数据库中检索数据(项目名称、价格和图像),并显示在列表视图中。我将图像存储在drawable文件夹(kids.jpg)中,并将图片的名称存储为字符串(kids)

listview的内容将根据查询条件不断变化

我现在面临的问题是,我可以检索图像的名称、价格甚至名称,并在listview中列出它,但我不知道如何在listview中显示图像

我知道下面的代码可以用来从字符串中调用drawable image,但是如何将代码插入到listview中呢

int Image = getResources().getIdentifier("com.example.user.birthdaygiftpredictor:drawable/" + Gimage, null, null);
            gift_image.setImageResource(Image);  
下面是我的SimpleCursorAdapter代码

Cursor cursor = db.rawQuery("SELECT * FROM gift WHERE "+strValue+" OR hobby LIKE '%"+hobby+"%' OR gift_price<='"+budget+"' ORDER BY gift_price ASC ;",null);

    String[] from = new String[] {
            cursor.getColumnName(0),
            cursor.getColumnName(1),
            cursor.getColumnName(2),
            cursor.getColumnName(4),
            cursor.getColumnName(4),
            cursor.getColumnName(5),
            cursor.getColumnName(10)

    };
    int[] to = new int[] {
            R.id.gift_id,
            R.id.gift_name,
            R.id.gift_price,
            R.id.gift_image,
            R.id.image,
            R.id.gift_description,
            R.id.link

    };

SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            Result_1_match.this, R.layout.view_gift_list, cursor, from, to);

 adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){

    public boolean setViewValue(View view, Cursor cursor, int columnIndex){

        if(view.getId() == R.id.gift_image){ // your ImageView id in xml
            DbHelper2 connect = new DbHelper2(Result_1_match.this);
            connect.openDataBase();
            SQLiteDatabase db = connect.getWritableDatabase();
            List<String> a = new ArrayList<>();

            Cursor c = db.rawQuery("your query;", null);
            int i=0;
            while (c.moveToNext()) {
                a.add(i,c.getString(0));
                i++;
            }
            ImageView imageView=(ImageView) view.findViewById(R.id.gift_image);
            int resID = getApplicationContext().getResources().getIdentifier(cursor.getString(columnIndex), "drawable",  getApplicationContext().getPackageName());
            imageView.setImageResource(resID);

            return true; //true because the data was bound to the view
        }
        return false;
    }
});

 list.setEmptyView(findViewById(R.id.imageView3));
    list.setAdapter(adapter);

Cursor Cursor=db.rawQuery(“从礼品中选择*,其中“+strValue+”或类似于“%”+hobby+“%”的爱好或礼品价格为此,您需要创建一个类似于此的自定义适配器,并设置为列表视图:

公共类CustomList扩展了ArrayAdapter{

private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;

}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true); TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);         

ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);

imageView.setImageResource(imageId[position]);
return rowView;
}
}
此链接提供完整的实施:

为此,您需要创建一个与此类似的自定义适配器,并设置为列表视图:

公共类CustomList扩展了ArrayAdapter{

private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;

}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true); TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);         

ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);

imageView.setImageResource(imageId[position]);
return rowView;
}
}
此链接提供完整的实施:

来自谷歌文档中的
SimpleCursorAdapter

SimpleCursorAdapter是一个简单的适配器,用于将列从光标映射到 XML文件中定义的文本视图或图像视图。您可以指定 要显示的列、要显示这些列的视图以及 定义这些视图外观的XML文件。绑定发生在 两个阶段。首先,如果SimpleCorsorAdapter.ViewBinder可用, setViewValue(android.view.view、android.database.Cursor、int)为 调用。如果返回值为true,则表示已发生绑定。如果 返回值为false,并且要绑定的视图是TextView, 调用setViewText(TextView,String)。如果返回值为 false,并且要绑定的视图是ImageView、setViewImage(ImageView、, 调用。如果找不到适当的绑定,则 引发IllegalStateException

因此,您可以轻松覆盖相应的
setViewText(TextView,String)
setviewmimage(ImageView,String)
方法。但是如果您仍然不知道如何做,您可以简单地将
ViewBinder
添加到列表适配器中

    adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
   /** Binds the Cursor column defined by the specified index to the specified view */
   public boolean setViewValue(View view, Cursor cursor, int columnIndex){
       if(view.getId() == R.id.your_image_view_id){ // your ImageView id in xml
            ImageView imageView=(ImageView) view;
                     int resID = getApplicationContext().getResources().getIdentifier(cursor.getString(columnIndex), "drawable",  getApplicationContext().getPackageName());
                     imageView.setImageDrawable(getApplicationContext().getResources().getDrawable(resID));
           return true; //true because the data was bound to the view
       }
       return false;
   }
});

它在谷歌的
SimpleCursorAdapter
文档中写道

SimpleCursorAdapter是一个简单的适配器,用于将列从光标映射到 XML文件中定义的文本视图或图像视图。您可以指定 要显示的列、要显示这些列的视图以及 定义这些视图外观的XML文件。绑定发生在 两个阶段。首先,如果SimpleCorsorAdapter.ViewBinder可用, setViewValue(android.view.view、android.database.Cursor、int)为 调用。如果返回值为true,则表示已发生绑定。如果 返回值为false,并且要绑定的视图是TextView, 调用setViewText(TextView,String)。如果返回值为 false,并且要绑定的视图是ImageView、setViewImage(ImageView、, 调用。如果找不到适当的绑定,则 引发IllegalStateException

因此,您可以轻松覆盖相应的
setViewText(TextView,String)
setviewmimage(ImageView,String)
方法。但是如果您仍然不知道如何做,您可以简单地将
ViewBinder
添加到列表适配器中

    adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
   /** Binds the Cursor column defined by the specified index to the specified view */
   public boolean setViewValue(View view, Cursor cursor, int columnIndex){
       if(view.getId() == R.id.your_image_view_id){ // your ImageView id in xml
            ImageView imageView=(ImageView) view;
                     int resID = getApplicationContext().getResources().getIdentifier(cursor.getString(columnIndex), "drawable",  getApplicationContext().getPackageName());
                     imageView.setImageDrawable(getApplicationContext().getResources().getDrawable(resID));
           return true; //true because the data was bound to the view
       }
       return false;
   }
});

我已通过插入您提供的代码更新了问题中的代码。不幸的是,图像仍然无法显示。我在调试模式下运行,剩余值为0。请再次检查我的代码并更正我的错误。感谢您的帮助。此游标的值是多少。getString(columnIndex)调试时,它是正确的图像名称吗?我得到的值是4,而不是图像名称。简而言之,您无法从数据库中获取正确的图像名称。您能否简单地调试光标,以检查图像名称是否出现在光标中,以及该图像在光标中的索引?如果查询结果有5条记录,这意味着我在listview中有5个项目,那么在调试模式下我应该得到哪个映像名?我已经通过插入您提供的代码更新了问题中的代码。不幸的是,映像仍然无法显示。我在调试模式下运行,剩余值为0。您能再次检查我的代码并更正我的错误吗?感谢您的帮助帮助。这个游标的值是什么。getString(columnIndex)调试时,它是正确的图像名称吗?我得到的值是4,而不是图像名称。简而言之,您无法从数据库中获取正确的图像名称。您能否简单地调试光标,以检查图像名称是否出现在光标中,以及该图像在光标中的索引?如果查询结果有5条记录,这意味着我在listview中有5个项目,那么在调试模式下我应该得到哪个映像名呢?