我想根据数据库值在 ListView 中显示一个图标。我跟随this https://stackoverflow.com/questions/2192082/android-changing-an-imageview-src-depending-on-database-field-data/2193727#2193727回答这样做。
但结果什么也没有显示。这是我的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;
}
} );
我特别在代码中使用与默认图标相同的图标。
这里有什么问题吗? (我有star_off
in drawable-hdpi
and drawable-mdpi
仅文件夹)
Upd。下面的代码运行良好:
movieIcon.setImageDrawable(getResources().getDrawable(R.drawable.star_off));
公共无效setImageResource(int resId) http://developer.android.com/reference/android/widget/ImageView.html#setImageResource%28int%29:这会在 UI 线程上进行位图读取和解码,这可能会导致延迟。如果这是一个问题,请考虑使用 setImageDrawable(Drawable) 或 setImageBitmap(Bitmap) 和 BitmapFactory 代替。
尝试使图像视图无效,或使用文档建议的替代方案之一。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)