Dynamically show images from resource/drawable

11,487

Solution 1

The SimpleAdapter expects an integer or string that specifies a resource or image URI:

public void setViewImage (ImageView v, String value)
Since: API Level 1

Called by bindView() to set the image for an ImageView but only if there is no existing ViewBinder or if the existing ViewBinder cannot handle binding to an ImageView. By default, the value will be treated as an image resource. If the value cannot be used as an image resource, the value is used as an image Uri. This method is called instead of setViewImage(ImageView, int) if the supplied data is not an int or Integer.

I believe should use setViewBinder to provide a ViewBinder for the SimpleAdapter to handle binding the Drawable data to the ImageView. Basically, the setViewValue() method should return false unless it is called for your image view. When it is called for your image view, the method should set the data in the view and return true. The return value indicates whether the ViewBinder was able to set the view or whether the adapter should try to bind the data itself via its default behavior.

Maybe something like:

private final SimpleAdapter.ViewBinder mViewBinder =
    new SimpleAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(
                final View view,
                final Object data,
                final String textRepresentation) {

            if (view instanceof ImageView) {
                ((ImageView) view).setImageDrawable((Drawable) data);
                return true;
            }

            return false;
        }
    };

...

listSetup .setViewBinder(mViewBinder);

I've done a similar thing with a SimpleCursorAdapter and its ViewBinder.

Solution 2

The simple adapter expects String in the map. But your map contains a drawable for the key "img".

Instead try extending the BaseAdapter class. Override the getView method and then manually set the drawable to the imageview and text to textview.

Another thought, it might not be a good idea to keep every drawable in memory. Rather get the drawable dynamically while rendering that particular row of the list

Share:
11,487
user601302
Author by

user601302

Updated on June 04, 2022

Comments

  • user601302
    user601302 about 2 years

    I'm trying to put different images (.jpg , .png) dynamically into a ListView from res/drawable . The names from the images I get from a database. The images themselves are in the res/drawable folder.

    This is what I already have, With an error of :D

    String imgName; --> There are the img names I need from the database

    Drawable drawable;    
        drawable = Class.appContext.getResources().getDrawable(Class.appContext.getResources().getIdentifier("com.example.test:drawable/"+imgName,null,null));  
    

    Then I get this into a ArrayList for every image in the database(+- 200 images):

    ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();
    
    map = new HashMap<String, Object>();
    map.put("img",drawable);
    
              map.put("text", "some text");
              list.add(map);
    

    Now I RETURN list;

    In the Class where I call :

    listReceive = Class.getImages(appContext); 
    
    listSetup = new SimpleAdapter(this, listReceive, R.layout.row,
                            new String[] {"img", "text"}, new int[] {R.id.IMG_CELL, R.id.TEXT_CELL});
                    lvList.setAdapter(listSetup);
    

    XML row is an ImageView and a TextView.

    System.out :resolveUri failed on bad bitmap uri: android.drawable.bitmapDrawable@405739 resolveUri failed on bad bitmap uri: android.drawable.bitmapDrawable@405639 resolveUri failed on bad bitmap uri: android.drawable.bitmapDrawable@405959 resolveUri failed on bad bitmap uri: android.drawable.bitmapDrawable@405677... ... ...

    I got it working when I saved images into local or SDcard memory, and then put the path inside the arraylist like:

    map.put("img","/data/data/com.example.test/images/" + imgName);
    

    I can't use this because then i will need to copy the pictures from res/drawable onto local or SD.This takes up 2 times the memory. can't have that of.

    There must be a way to get images dynamically from the drawable.

    Any one knows what I'm missing here? Thanks.

  • user601302
    user601302 over 13 years
    Thanks ,I'm definitely going to use baseAdapters in the future. Seems very handy.
  • user601302
    user601302 over 13 years
    Dude,You are awesome!! I just implemented your solution and it works like a sharm !! Thanks !!
  • LA_
    LA_ over 13 years
    thanks a lot for your explanation on what setViewValue should return!
  • Bert F
    Bert F about 13 years
    @LA_ - I'm pleased my answer was helpful. Thanks for the feedback.