【问题标题】:How would I populate a listview with images from drawable using a path from a database?如何使用数据库中的路径使用可绘制的图像填充列表视图?
【发布时间】:2016-01-11 20:11:09
【问题描述】:

现在,我有一个列表视图,其中填充了现有数据库中的数据。它仅填充文本,但我希望它也显示图像。在数据库中,我将每个条目对应的图像的图像路径存储在表中。图像在drawable中。我不知道如何编辑它,所以它也会显示图像。

我这样做imageview.setImageResource(getDrawable(rootView.getContext(),imagePath)); 将图像设置在不同的活动中,所以我认为它会相似,但我不确定。

    private void displayListView() {

        final Cursor cursor = dbHelper.fetchAllRecipes();

        // The desired columns to be bound
        String[] columns = new String[]{
                //DBHandler.COLUMN_CODE,
                DBHandler.COLUMN_NAME,
                DBHandler.COLUMN_TYPE,
                DBHandler.COLUMN_INGRED,
                DBHandler.COLUMN_SPECIAL
        };

        // the XML defined views which the data will be bound to
        int[] to = new int[]{
                //R.id.code,
                R.id.name,
                R.id.type,
                R.id.ingredient,
                R.id.special
        };

        // create the adapter using the cursor pointing to the desired data
        //as well as the layout information
        dataAdapter = new SimpleCursorAdapter(
                this, R.layout.recipeinfo,
                cursor,
                columns,
                to,
                0);

        final ListView listView = (ListView) findViewById(R.id.listView1);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);
      }

    public static int getDrawable(Context context, String name)
    {
        Assert.assertNotNull(context);
        Assert.assertNotNull(name);

        return context.getResources().getIdentifier(name,
                "drawable", context.getPackageName());
    }

recipeinfo.xml(列表视图中每个元素的格式)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="6dip"
            android:id="@+id/recipe_layout">
<GridLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:id="@+id/image"
        android:layout_column="0"
        android:layout_row="0"
        android:layout_rowSpan="4"
        android:adjustViewBounds="true"
        android:baselineAlignBottom="false"
        android:cropToPadding="false"
        android:nestedScrollingEnabled="false"
        android:layout_marginRight="15dp"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_column="1"
        android:layout_row="0"
        android:textColor="@android:color/black"/>

    <TextView
        android:id="@+id/type"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_column="1"
        android:layout_row="1"/>

    <TextView
        android:id="@+id/ingredient"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_column="1"
        android:layout_row="2"/>

    <TextView
        android:id="@+id/special"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_column="1"
        android:layout_row="3"/>

</GridLayout>

【问题讨论】:

  • In the database, I stored the image path in the table for the corresponding image of each entrydrawable 中的图像的路径如何?
  • 它只是一个与文件名匹配的字符串。例如:createRecipe("Honey Glazed Salmon”,...,"honeyglazedsalmon");图片名称为“honeyglazedsalmon”
  • 现在有什么问题?
  • 我想从listview中的图片路径显示图片
  • 这就是你想要的。但问题是什么?好吧 setImageResource 会做到的。

标签: android image listview


【解决方案1】:

我想通了。我刚刚制作了一个自定义适配器,就像 greenapps 建议的那样。

public class CustomCursorAdapter extends CursorAdapter {
    public CustomCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

    // The newView method is used to inflate a new view and return it,
    // you don't bind any data to the view at this point.
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.recipeinfo, parent, false);
    }

    // The bindView method is used to bind all data to a given view
    // such as setting the text on a TextView.
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // Find fields to populate in inflated template
        ImageView image = (ImageView)view.findViewById(R.id.image);
        TextView name = (TextView) view.findViewById(R.id.name);
        TextView type = (TextView) view.findViewById(R.id.type);
        TextView ingredient = (TextView) view.findViewById(R.id.ingredient);
        TextView special = (TextView) view.findViewById(R.id.special);
        // Extract properties from cursor
        String imagePath = cursor.getString(cursor.getColumnIndexOrThrow("imgpath"));
        String recipeName = cursor.getString(cursor.getColumnIndexOrThrow("name"));
        String recipeType = cursor.getString(cursor.getColumnIndexOrThrow("type"));
        String recipeMainIngredient = cursor.getString(cursor.getColumnIndexOrThrow("ingred"));
        String recipeSpecial = cursor.getString(cursor.getColumnIndexOrThrow("special"));
        // Populate fields with extracted properties
        image.setImageResource(getDrawable(view.getContext(), imagePath));
        name.setText(recipeName);
        type.setText(recipeType);
        ingredient.setText(recipeMainIngredient);
        special.setText(recipeSpecial);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2012-10-26
    • 1970-01-01
    相关资源
    最近更新 更多