我想在ListView中显示一个图标,具体取决于数据库值.我按照this的答案这样做.
但结果没有显示任何内容.这是我在row.xml中的内容:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="@+id/movie_subscribed_icon"
android:padding="3dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/star_off"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
...
这是代码:
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)
UPD.以下代码运行良好:
movieIcon.setImageDrawable(getResources().getDrawable(R.drawable.star_off));
解决方法:
public void setImageResource (int resId):这在UI线程上进行Bitmap读取和解码,这可能导致延迟打嗝.如果这是一个问题,请考虑使用setImageDrawable(Drawable)或setImageBitmap(Bitmap)和BitmapFactory.
尝试使图像视图无效,或使用文档建议的替代方法之一.
标签:android,android-listview,resources,android-imageview
来源: https://codeday.me/bug/20190610/1211290.html