为什么setImageResource没有显示任何内容?

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;
        }

      } );

我在我的代码中特别使用与默认图标相同的图标。

这里有什么问题吗?(我只在drawable-hdpidrawable-mdpi文件夹中有star_off

更新。以下代码有效:

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

顺便提一下,我在LogCat中看到以下内容: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_
5个回答

6

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

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


请问您能否澄清一下"invalidating"是什么意思? - LA_
绘图通过遍历树并渲染与无效区域相交的每个视图来处理。由于树是按顺序遍历的,这意味着父项将在其子项之前(即在其后面)绘制,兄弟项按它们在树中出现的顺序绘制。如果为视图设置了背景可绘制对象,则该视图将在调用其onDraw()方法之前为您绘制它。请注意,框架不会绘制不在无效区域内的视图。要强制绘制视图,请调用invalidate()。 - Ian
嗯,出现了一些奇怪的情况。它开始使用setImageDrawable工作,但现在又不起作用了。如果我在setImageDrawable之后立即调用movieIcon.invalidate(); - 它也没有帮助。 - LA_

2
我在另一个问题中找到了解决方案。
基本上,setViewValue()方法应该在调用图像视图之前返回false。然后它应该设置视图中的数据并返回true。返回值表示ViewBinder是否自己设置了视图,还是适配器应该通过其默认行为自己绑定数据。
由于我没有返回true,所以它工作不正确。现在它完美地工作了。

1
这段代码解决了问题:
ImageView movieIcon = (ImageView)findViewById(R.id.movie_subscribed_icon);
Drawable drawable = this.getResources().getDrawable(R.drawable.android);
movie_subscribed_icon.setBackgroundDrawable(drawable);

1

尝试第二步。 为什么不试试这个...

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

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

-1

这个有效...

在 XML 中,不要使用 ImageView drawable 的 "src" 属性。相反,使用 "background"。

像这样:

<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替换为drawable文件夹中图像的名称。不要忘记调用invalidate()。

注意:不要使用setBackgroundDrawable(),因为它已经过时了。

希望这对其他人也有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接