我想在我的ListView中根据数据库的值显示一个图标。我遵循这个答案去实现。但是结果什么都没有显示。以下是我在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));
WARN/ImageView(7293): Unable to find resource: 2 WARN/ImageView(7293): android.content.res.Resources$NotFoundException: Resource ID #0x2
- LA_R.drawable.star_off
更改为android.R.drawable.star_off
,我仍然遇到这个问题和警告。 - LA_