5

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

      } );

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

更新。以下代码运行良好:

movieIcon.setImageDrawable(getResources().getDrawable(R.drawable.star_off));
4

5 回答 5

5

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

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

于 2011-03-26T17:07:35.000 回答
2

我在另一个问题中找到了解决方案。

基本上 setViewValue() 方法应该返回 false ,直到它为您的图像视图调用。然后它应该在视图中设置数据并返回true。返回值指示 ViewBinder 是否设置视图本身或适配器是否应通过其默认行为绑定数据本身。

由于我没有返回 true,所以它工作不正确。现在它完美地工作了。

于 2011-03-28T19:51:58.413 回答
1

尝试#2。你为什么不试试这个...

ImageView movieIcon = (ImageView)findViewById(R.id.movie_subscribed_icon);
movieIcon.setImageResource(R.drawable.star_off);

我认为您可能正在创建一个新的 ImageView 而不是抓取布局中已经存在的那个。

于 2011-03-26T21:13:35.663 回答
1

这段代码解决了这个问题:

ImageView movieIcon = (ImageView)findViewById(R.id.movie_subscribed_icon);
Drawable drawable = this.getResources().getDrawable(R.drawable.android);
movie_subscribed_icon.setBackgroundDrawable(drawable);
于 2011-05-17T00:39:29.070 回答
-1

这有效...

在 XML 中,不要对 ImageView drawable 使用“src”属性。相反,使用“背景”。

像这样:

<ImageView
        android:id="@+id/maximize_inbox_window"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_action_original_image"
        android:layout_gravity="center_vertical"
        android:clickable="true"
        android:paddingRight="5dp" />

在运行时更改图像的 Java 代码在这里......

ImageView img = (ImageView) rootView.findViewById(R.id.image_view_name);
img.setBackground(getResources().getDrawable(R.drawable.resource_id));
img.invalidate();

将 image_view_name 替换为 XML 中的 ImageView,将 resource_id 替换为可绘制文件夹中的图像名称。不要忘记调用 invalidate()。

注意:不要使用 setBackgroundDrawable() 作为它的弃用。

希望这也适用于其他人。

于 2014-08-04T10:16:38.387 回答