【发布时间】: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 会做到的。