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

时间:2016-01-19 18:49:50

标签: android listview

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

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

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

我知道下面的代码可以用来从字符串调用drawable图像,但是我如何将代码插入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);

更新:最后我解决了问题并使用Mukesh Rana概念成功列出了图像。以上是正确的代码。非常感谢你:))

2 个答案:

答案 0 :(得分:0)

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

公共类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;
}
}

此链接提供完整的实施:
http://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html

答案 1 :(得分:0)

来自SimpleCursorAdapter的Google文档,它说

  

SimpleCursorAdapter是一个简单的适配器,用于将游标中的列映射到   在XML文件中定义的TextViews或ImageViews。您可以指定哪个   您想要的列,您想要显示列的视图,以及   定义这些视图外观的XML文件。绑定发生在   两个阶段。首先,如果SimpleCursorAdapter.ViewBinder可用,   setViewValue(android.view.View,android.database.Cursor,int)是   调用。如果返回的值为true,则发生绑定。如果   返回值为false,要绑定的视图是TextView,   调用setViewText(TextView,String)。如果返回值是   false和bind的视图是一个ImageView,setViewImage(ImageView,   字符串)被调用。如果找不到合适的绑定,   抛出IllegalStateException。

因此,您可以轻松覆盖相应的setViewText(TextView,String)setViewImage(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;
   }
});