android - 如何将图像从 Drawable 加载到 SimpleCursorAdapter

标签 android sqlite android-drawable simplecursoradapter

我在该适配器中使用 Simple Cursor Adapter 创建了一个 ListView 我现在获取我的 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);

最佳答案

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

你的类.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;
        }
    });

列表行.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>

关于android - 如何将图像从 Drawable 加载到 SimpleCursorAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33452195/

相关文章:

android - Flutter:将 Proguard 与 Gradle 7.0.4 结合使用

android - 使用 ADB shell 设置日期/时间

ruby-on-rails - 即使有要返回的记录,Rails find 也会返回 nil

android - 关于 android 可绘制文件夹的说明

java - 如何在不更改 RecyclerView 中的主体的情况下更改 View Drawable 的背景颜色?

java - 非 Activity 扩展类上的 SharedPreferences

java - Android:Dagger 2 会干扰 Immutables?

c++ - 如何从数据库解析日期时间格式?

ios - 数据库执行查询错误 : SQL logic error or missing database xcode

android - 以编程方式更改单选按钮的背景