Dynamically show images from resource/drawable
I'm trying to put different images (.jpg , .png) dynamically into a ListView开发者_如何学JAVA
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.
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.
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
This following method or way also works - if anyone is still interested
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
Integer flags[] = new Integer[listdetail.size()];
Resources resources = getResources();
for (int i = 0; i < listdetail.size(); i += 2) {
listName = listdetail.get(i).getName();
int dynamicId = resources.getIdentifier(listdetail.get(i).getName().replaceAll(" ", "").toLowerCase(), "drawable", getActivity().getPackageName());
if(dynamicId == 0) {
dynamicId = R.drawable.test_image;
flags[i] = dynamicId;
}else {
flags[i] = dynamicId;
}
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("value1", listdetail.get(i).getName());
hm.put("valueImage1", Integer.toString(flags[i]));
list.add(hm);
}
String[] from = { "value1", "valueImage1"};
int[] to = { R.id.textview, R.id.imageView };
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), list, R.layout.listlayout, from, to);
ListView listView = (ListView) getActivity().findViewById(R.id.listview);
listView.setAdapter(adapter);
精彩评论