Why setImageResource displays nothing?
Solution 1
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.
Solution 2
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.
Solution 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.
Solution 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);
LA_
To search in your posts only type: user:me text To list all your unaccepted questions, type: user:me hasaccepted:0 My questions list: link
Updated on June 20, 2022Comments
-
LA_ almost 2 years
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
indrawable-hdpi
anddrawable-mdpi
folders only)Upd. the following code works well:
movieIcon.setImageDrawable(getResources().getDrawable(R.drawable.star_off));
-
LA_ about 13 yearsBtw, I see the following in LogCat:
WARN/ImageView(7293): Unable to find resource: 2 WARN/ImageView(7293): android.content.res.Resources$NotFoundException: Resource ID #0x2
-
LA_ about 13 yearsAnd even if I change
R.drawable.star_off
toandroid.R.drawable.star_off
I have this problem and such warning.
-
-
LA_ about 13 yearsCould you please clarify what 'invalidating' means?
-
Ian about 13 years"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(). "
-
LA_ about 13 yearsHmm. Something strange happens. It is started to work with
setImageDrawable
, but now it doesn't work again. If I callmovieIcon.invalidate();
immediately aftersetImageDrawable
- it doesn't help.