【问题标题】:How to retrieve image from local database and display in ListView (Android)如何从本地数据库中检索图像并在 ListView (Android) 中显示
【发布时间】:2016-01-19 18:49:49
【问题描述】:

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

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 概念成功列出了图像。以上是正确的代码。非常感谢:)

【问题讨论】:

    标签: android listview


    【解决方案1】:

    来自SimpleCursorAdapter 的谷歌文档,它说

    SimpleCursorAdapter 是一个简单的适配器,可以将列从光标映射到 在 XML 文件中定义的 TextViews 或 ImageViews。您可以指定哪个 您想要的列,要显示列的视图,以及 定义这些视图外观的 XML 文件。绑定发生在 两个阶段。首先,如果 SimpleCursorAdapter.ViewBinder 可用, setViewValue(android.view.View, android.database.Cursor, int) 是 调用。如果返回值为 true,则表示发生了绑定。如果 返回值为 false 并且要绑定的视图是 TextView, 调用 setViewText(TextView, String)。如果返回值为 false 并且要绑定的视图是 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;
       }
    });
    

    【讨论】:

    • 我已通过插入您提供的代码更新了问题中的代码。不幸的是,图像仍然无法显示。我在调试模式下运行,resID 为 0。如果我错了,请您再次检查我的代码并纠正我。感谢您的帮助。
    • 调试时这个 cursor.getString(columnIndex) 的值是多少,它是正确的图像名称吗?
    • 我得到的值是 4 而不是图像名称
    • 所以简而言之,您无法从数据库中获取正确的图像名称。您可以简单地调试您的光标以检查光标中是否出现图像名称以及光标中该图像的索引吗?
    • 如果查询结果有5条记录,表示我的listview有5条,那么在debug模式下应该取哪个图片名呢?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      相关资源
      最近更新 更多