Why setImageResource displays nothing?

余生颓废 提交于 2020-01-02 06:11:11

问题


I would like to display an icon in my ListView depending on the database value. I follow this answer to do so. But in result nothing is displayed. Here is what I have in my 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"
     ...

and here is the code:

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

      } );

I especially use the same icon in my code as default one. What is wrong here? (I have star_off in drawable-hdpi and drawable-mdpi folders only)

Upd. the following code works well:

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

回答1:


public void setImageResource (int resId) : This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(Drawable) or setImageBitmap(Bitmap) and BitmapFactory instead.

Try invalidating your image view, or use one of the alternatives that the documentation suggests.




回答2:


I've found a solution in another question.

Basically the setViewValue() method should return false until it is called for your image view. Then it should set the data in the view and return true. The return value indicates whether the ViewBinder set the view itself or whether the adapter should bind the data itself via its default behavior.

Since I didn't return true, it worked incorrectly. Now it works perfectly.




回答3:


Attempt #2. Why don't you try this...

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

I think you may be creating a new ImageView instead of grabbing the one that already exists in your layout.




回答4:


This piece of code solves the problem :

ImageView movieIcon = (ImageView)findViewById(R.id.movie_subscribed_icon);
Drawable drawable = this.getResources().getDrawable(R.drawable.android);
movie_subscribed_icon.setBackgroundDrawable(drawable);



回答5:


This works...

In the XML, do not use "src" attribute for ImageView drawable. Instead, use "background".

Like this:

<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" />

And the Java code to change the image at runtime is here...

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

Substitute the image_view_name with your ImageView in the XML and the resource_id with your name of the image in the drawable folders. Don't forget to call invalidate().

Note: Do not use setBackgroundDrawable() as its deprecated.

Hope this works for others too.



来源:https://stackoverflow.com/questions/5443691/why-setimageresource-displays-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!