我想根据数据库值在我的 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;
        }

      } );

我特别在我的代码中使用相同的图标作为默认图标。
这里有什么问题? (我只有 star_offdrawable-hdpi 文件夹中有 drawable-mdpi)

更新 。以下代码运行良好:
movieIcon.setImageDrawable(getResources().getDrawable(R.drawable.star_off));

最佳答案

public void setImageResource (int resId) :这会在 UI 线程上执行 Bitmap 读取和解码,这可能会导致延迟打嗝。如果这是一个问题,请考虑使用 setImageDrawable(Drawable) 或 setImageBitmap(Bitmap) 和 BitmapFactory 代替。

尝试使您的 ImageView 无效,或使用文档建议的替代方法之一。

关于android - 为什么 setImageResource 什么都不显示?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5443691/

10-12 00:12
查看更多