【发布时间】:2011-03-26 16:37:01
【问题描述】:
我想根据数据库值在我的 ListView 中显示一个图标。我按照this 回答这样做。 但结果什么都没有显示。这是我的 row.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/movie_subscribed_icon"
android:padding="3dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/star_off"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/movie_name"
...
这里是代码:
movies.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch(viewId) {
case R.id.movie_name:
int readValue = cursor.getInt(cursor.getColumnIndexOrThrow(MoviesDbAdapter.KEY_READ));
if (readValue == 1) { // viewed movie item
TextView movieName = (TextView) view;
movieName.setTextColor(Color.GRAY);
}
break;
case R.id.movie_subscribed_icon:
int subscribedValue = cursor.getInt(cursor.getColumnIndexOrThrow(MoviesDbAdapter.KEY_READ));
if (subscribedValue > 0) { // subscribed movie item
ImageView movieIcon = (ImageView) view;
movieIcon.setImageResource(R.drawable.star_off);
}
break;
}
return false;
}
} );
我特别在我的代码中使用与默认图标相同的图标。
这里有什么问题? (我只有在drawable-hdpi 和drawable-mdpi 文件夹中有star_off)
更新。以下代码运行良好:
movieIcon.setImageDrawable(getResources().getDrawable(R.drawable.star_off));
【问题讨论】:
-
顺便说一句,我在 LogCat 中看到以下内容:
WARN/ImageView(7293): Unable to find resource: 2 WARN/ImageView(7293): android.content.res.Resources$NotFoundException: Resource ID #0x2 -
即使我将
R.drawable.star_off更改为android.R.drawable.star_off我也有这个问题和这样的警告。
标签: android resources android-listview android-imageview