Reputation: 1181
I currently have a simple list view adapter that holds two rows of text. What I am trying to do next is add the option of displaying a photo that the user took in the list view. I modified my list adapter like so:
standardAdapter = new SimpleAdapter(this, list, R.layout.post_layout,
new String[] { "time", "post", "image"}, new int[] {
R.id.postTimeTextView, R.id.postTextView, R.id.post_imageView});
Then I add it to the hash map as usual and refresh the adapter:
// create a new hash map with the text from the post
feedPostMap = new HashMap<String, Object>();
feedPostMap.put("time", currentTimePost);
feedPostMap.put("post", post);
if(photoWasTaken == 1){
feedPostMap.put("image", pictureTaken);
}
//add map to list
list.add(feedPostMap);
// refresh the adapter
standardAdapter.notifyDataSetChanged();
Lastly, here is the code for activity on result:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "ON activity for result- CAMERA");
if (resultCode == Activity.RESULT_OK) {
//get and decode the file
pictureTaken = BitmapFactory.decodeFile("/sdcard/livefeedrTemp.png");
//Display picture above the text box
imageViewShowPictureTaken.setImageBitmap(pictureTaken);
displayPhotoLayout.setVisibility(LinearLayout.VISIBLE);
//NEW - make photo variable = 1
photoWasTaken = 1;
}
}
However I am running into an issue. The photo, in bitmap form, is not being added to the list view. It just appears as empty white space. Am I doing something wrong here? Secondly, if the user decided not to take a picture, then the image view should not be displayed. I'm not sure how to implement this. Should I create a custom list adapter?
Thanks for your help
Upvotes: 2
Views: 921
Reputation: 4081
The problem is that SimpleAdapter don't support Bitmaps by default.
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.
There is a solution, however. You can set a custom ViewBinder and make the binding by yourself.
class MyViewBinder implements SimpleAdapter.ViewBinder {
@Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
if (view instanceof ImageView && data instanceof Bitmap) {
ImageView v = (ImageView)view;
v.setImageBitmap((Bitmap)data);
// return true to signal that bind was successful
return true;
}
return false;
}
}
And set this to your SimpleAdapter:
adapter.setViewBinder(new MyViewBinder());
This way every time the SimpleAdapter tries to bind a value to a View it first calles your View binder's setViewValue method. If it returns false, it tries to bind it itself.
You can also try to put into your map an URL as a string pointing to the sd card location. I'm not sure however, that the SimpleAdapter can handle this.
Also see this post: Dynamically show images from resource/drawable
Upvotes: 2