【问题标题】:Have image from drawable folder show up in listview using simplecursoradapater使用 simplecursoradapater 将可绘制文件夹中的图像显示在列表视图中
【发布时间】:2012-12-30 14:52:46
【问题描述】:

我想要做的是使用 SimpleCursorAdapter 将我的 drawable-dbimages 文件夹中的图像显示在 ImageView 中。

我真的不知道该怎么做。我知道如何使用BitmapFactory.decodeResource 在数据库中通过其名称获取图像,但我不知道如何将其应用于适配器。

例如,假设我有一个名为cars 的表。在该表中,我有一个名为image 的列。每行的image 的值是drawable-dbimages 文件夹中的图像名称。

现在我有了这个代码:

cursor = datasource.fetchAllCars();
to = new int[] { R.id.listitem_car_name, R.id.listitem_car_image };
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_car, cursor, columns, to, 0);
setListAdapter(dataAdapter);

R.id.listitem_car_name 是文本视图,R.id.listitem_car_image 是图像视图。

我知道如何从数据库中获取 image 的值并将其吐出到 textview 中,但我希望它具有来自 drawables 文件夹的图像,其名称在数据库列中,显示在每个列表视图项的图像视图。

我不知道该怎么做。

【问题讨论】:

    标签: android sqlite imageview simplecursoradapter


    【解决方案1】:

    android 的预制SimpleCursorAdapter 仅支持TextViews 并将光标列映射到它们。对于您所描述的内容,您需要制作自己的适配器对象,这里我使用了CursorAdapter,这需要通过一些幕后工作来弄脏您的手。这是我的示例中的主要实例:

        cursor = datasource.fetchAllCars();
        dataAdapter = new CustomCursorAdapter(this, cursor, 0);
        setListAdapter(dataAdapter);
    

    然后这里是完整的对象

    import android.content.Context;
    import android.database.Cursor;
    import android.support.v4.widget.CursorAdapter;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class CustomCursorAdapter extends CursorAdapter {
    
        private LayoutInflater inflater;
    
        public CustomCursorAdapter(Context context, Cursor c, int flags) {
            super(context, c, flags);
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        @Override
        public View newView(Context context, Cursor c, ViewGroup parent) {
            // do the layout inflation here
            View v = inflater.inflate(R.layout.listitem_car, parent, false);
            return v;
        }
    
        @Override
        public void bindView(View v, Context context, Cursor c) {
            // do everything else here
            TextView txt = (TextView) v.findViewById(R.id.listitem_car_name);
            ImageView img = (ImageView) v.findViewById(R.id.listitem_car_image);
    
            String text = c.getString(c.getColumnIndex("COLUMN_TEXT")); 
            txt.setText(text);
    
            // where the magic happens
            String imgName = c.getString(c.getColumnIndex("COLUMN_IMAGE"));
            int image = context.getResources().getIdentifier(imgName, "drawable", context.getPackageName());
            img.setImageResource(image);
        }
    
    }
    

    我希望它主要是不言自明的,但是我标记为“魔法发生的地方”的部分应该是与您的问题相关的最重要的部分。基本上,您从数据库中获取图像名称,然后下一行尝试按名称(而不是像往常一样通过 id)查找图像,然后您只需像往常一样设置图像。该方法为它找不到的图像返回int 0,因此您可能希望也可能不希望为此执行错误处理。此外,如果您想使用其他加载图像的方法,那就是这样做的地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多