Why setImageResource displays nothing?
Asked Answered
A

5

5

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));
Amputate answered 26/3, 2011 at 16:37 Comment(2)
Btw, I see the following in LogCat: WARN/ImageView(7293): Unable to find resource: 2 WARN/ImageView(7293): android.content.res.Resources$NotFoundException: Resource ID #0x2Amputate
And even if I change R.drawable.star_off to android.R.drawable.star_off I have this problem and such warning.Amputate
A
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.

Amputate answered 28/3, 2011 at 19:51 Comment(0)
S
6

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.

Streamer answered 26/3, 2011 at 17:7 Comment(3)
Could you please clarify what 'invalidating' means?Amputate
"Drawing is handled by walking the tree and rendering each view that intersects the the invalid region. Because the tree is traversed in-order, this means that parents will draw before (i.e., behind) their children, with siblings drawn in the order they appear in the tree. If you set a background drawable for a View, then the View will draw it for you before calling back to its onDraw() method. Note that the framework will not draw views that are not in the invalid region. To force a view to draw, call invalidate(). "Counselor
Hmm. Something strange happens. It is started to work with setImageDrawable, but now it doesn't work again. If I call movieIcon.invalidate(); immediately after setImageDrawable - it doesn't help.Amputate
A
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.

Amputate answered 28/3, 2011 at 19:51 Comment(0)
S
1

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.

Streamer answered 26/3, 2011 at 21:13 Comment(0)
W
1

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);
Weatherworn answered 17/5, 2011 at 0:39 Comment(0)
A
-1

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.

Acree answered 4/8, 2014 at 10:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.