【问题标题】:How To load Images from Drawable to SimpleCursorAdapter如何将图像从 Drawable 加载到 SimpleCursorAdapter
【发布时间】:2015-10-31 13:33:46
【问题描述】:

我在该适配器中使用 Simple Cursor Adapter 创建一个列表视图我现在获取我的 Sqlite 列名称我想从 Drawable 文件夹中将图像添加到同一个适配器我们如何做到这一点给出想法和建议谢谢..

这是我的代码:

    protected void onCreate(Bundle savedInstanceState) {
           final int[] imageResource =new int[] {R.drawable.juice, R.drawable.medicinebowl, R.drawable.kam};
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView= (ListView) findViewById(R.id.listView);
     dbHelper = new SqlLiteDbHelper(this);
            try {
                dbHelper.openDataBase();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            sqLiteDatabase = dbHelper.getReadableDatabase();
            cursor = dbHelper.gettitles(sqLiteDatabase);
            String[] from = new String[]{dbHelper.TITLE, String.valueOf(circle)};
    int[] to = new int[]{R.id.title,R.id.circle};
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.title_row, cursor, from, to);
            adapter.notifyDataSetChanged();

            listView.setAdapter(adapter);

【问题讨论】:

  • 你的问题解决了吗
  • 我创建了一个对象来保存来自Drwable int[] imageResource = {R.drawable.juice, R.drawable.medicinebowl, R.drawable.kam}; 的图像并将其附加到我的适配器中,例如String[] from = new String[]{dbHelper.TITLE, String.valueOf(imageResource)}; @Tony
  • 您需要一个对象 ID 数组
  • 你能详细解释一下吗@Tony
  • 我已经编辑了我的答案

标签: android sqlite android-drawable simplecursoradapter


【解决方案1】:

为此,您需要自己的自定义布局并覆盖setViewBinder 方法,下面是一个可以帮助您理解的示例,

YourClass.java

// The desired columns to be bound
    String[] columns = new String[] { DBHelper.KEY_IMAGE_TYPE, DBHelper.KEY_HOUSE,
            DBHelper.KEY_PRICE };

    // the XML defined views which the data will be bound to
    int[] to = new int[] {  
            R.id.icon,
            R.id.bowler_txt};

    SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(this,
            R.layout.listrow, cursor, columns, to, 0);
    dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor,
                int columnIndex) {
            switch (view.getId()) {
            case R.id.image:
                int imageType = cursor.getInt(columnIndex);
                int imageToBeShown=0;
                switch (imageType) {
                case TYPE_ONE:
                    imageToBeShown=imgs[0];
                    break;
                case TYPE_TWO:
                    imageToBeShown=imgs[1];
                    break;
                case TYPE_THREE:
                    imageToBeShown=imgs[2];
                    break;
                case TYPE_FOUR:
                    imageToBeShown=imgs[3];
                    break;
                case TYPE_FIVE:
                    imageToBeShown=imgs[4];
                    break;
                case TYPE_SIX:
                    imageToBeShown=imgs[5];
                    break;
                }
                ((ImageView)view).setImageResource(imageToBeShown);
                return true;
            }
            return false;
        }
    });

listrow.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/bowler_txt"
    android:paddingLeft="25dp"
    android:textSize="30dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Bowler"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

【讨论】:

  • 在上面的代码中,imgs 是你的图像drawable ids的整数数组变量......!祝你好运
  • TYPE_ONE 指的是什么?? @Arsal 伊玛目
  • 可以在列表视图行中设置多张图片的类型字段
  • 如何使用行大小写取决于我的标题@ArsalImam
【解决方案2】:

我觉得你可以用这个方法

int[] imageResource = {R.drawable.image_1, R.drawable.image_2, R.drawable.image_3};

并将其传递给您的 SimpleCursorAdapter 构造函数。

【讨论】:

  • SimpleCursorAdapter 不包含 ImageView 本身...!
  • 那为什么你需要在适配器内部绘制可绘制对象?
  • 我创建了一个对象来保存来自Drwable int[] imageResource = {R.drawable.juice, R.drawable.medicinebowl, R.drawable.kam}; 的图像并将其附加到我的适配器中,例如String[] from = new String[]{dbHelper.TITLE, String.valueOf(imageResource)};@beardedbeast
【解决方案3】:

你可以简单地通过添加这一行来做到这一点

your_imageview.setImageResource(R.drawable.drwable_image);

如果您还没有创建扩展 SimpleCursorAdapter 的自定义适配器,您可以这样做

  String[] columns = new String[] { People.NAME, People.NUMBER };
int[] to = new int[] { R.id.name_entry, R.id.number_entry };

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_example_entry, cursor, columns, to);

colomns 数组应包含日期,to 数组应包含您要为其分配数据的对象的 id

这用于图像

    listAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){

   public boolean setViewValue(View view, Cursor cursor, int columnIndex){
       if(view.getId() == R.id.your_image_view_id){
          int imageType = cursor.getInt(columnIndex);
            int imageToBeShown=0;
            switch (imageType) {
            case TYPE_ONE:
                imageToBeShown=imgs[0];
                break;
            case TYPE_TWO:
                imageToBeShown=imgs[1];
                break;
            case TYPE_THREE:
                imageToBeShown=imgs[2];
                break;
            case TYPE_FOUR:
                imageToBeShown=imgs[3];
                break;
            case TYPE_FIVE:
                imageToBeShown=imgs[4];
                break;
            case TYPE_SIX:
                imageToBeShown=imgs[5];
                break;
            }
            ((ImageView)view).setImageResource(imageToBeShown);

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

设置图片资源应该取决于你的行类型

【讨论】:

  • 如何在适配器中附加?你能说详细点吗? @托尼
  • 你可以使用这个,老实说@Arsal 一开始就提到了这个
  • ((ImageView)view).setImageDrawable(imageresource);?? @托尼
  • setImageDrawable (android.graphics.drawable.Drawable) 在 ImageView 中无法应用到 (int[]) 这是我尝试应用时显示的内容((ImageView)view).setImageDrawable(imageresource);@Tony
  • 检查我更新的答案使用设置图像资源代替
猜你喜欢
  • 2013-09-11
  • 1970-01-01
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
相关资源
最近更新 更多