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 😀
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.
The SimpleAdapter expects an integer or string that specifies a resource or image URI:
I believe should use setViewBinder to provide a ViewBinder for the
SimpleAdapterto handle binding theDrawabledata to theImageView. 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:
I’ve done a similar thing with a SimpleCursorAdapter and its ViewBinder.