如何使用数据库中的路径使用drawable中的图像填充列表视图?

时间:2015-04-30 20:32:46

标签: android image listview

现在,我有一个列表视图,其中填充了现有数据库中的数据。它只是填充文本,但我希望它也显示图像。在数据库中,我将图像路径存储在表中,以用于每个条目的相应图像。图像在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(listview中每个元素的格式)

<?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>

1 个答案:

答案 0 :(得分:0)

我明白了。我刚刚制作了一个像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);
    }
}