Passing values to view with the adapter
I've implemented (thanks to a tutorial) a 开发者_StackOverflowway to show a custom listView, with images, names and description.
I used the simpleAdapter
to pass the strings name
and description
to the textViews, and the int of an image's Id (the image is stored in R.drawable) to the imageView.
The problem is: now i should set the ImageViews to display images downloaded from the web, so I have drawables but i can't get IDs from them.
How can i customize the simpleAdapter to pass a Drawable
to the imageView
, and set the image with the setImageDrawable
method?
Please be clear! Thanks
We need to use the viewBinder, as show here:
adapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if(data instanceof BitmapDrawable ){
((ImageView)view).setImageDrawable((Drawable)data);
return true;
}else{
return false;
}
}
});
Try This:
http://www.androidpeople.com/android-load-image-from-url-example/
Or to load from External media files you have already downloaded:
Bitmap myBitmap = BitmapFactory.decodeFile(“/sdcard/myImage.jpg”);
ImageView myImage = (ImageView) findViewById(R.id.imageToShow);
myImage.setImageBitmap(myBitmap);
This is quite difficult... so I would advise to use CommonsWare's ThumbnailAdapter.
精彩评论