Android ListView SimpleAdapter支持Bitmap类型图片显示
// 处理simpleAdapter中包括bitmap类型 adapter.setViewBinder(new ViewBinder() { public boolean setViewValue(View view, Object data, String textRepresentation) { if (view instanceof ImageView && data instanceof Bitmap) { ImageView image = (ImageView) view; image.setImageBitmap((Bitmap) data); return true; } return false; } });
完整示例如下:
listData = this.GetPersonalData(); adapter = new SimpleAdapter(this, listData, R.layout.personal_list_item, new String[] { "label", "image", "arrows", "value" }, new int[] { R.id.personal_label, R.id.personal_img, R.id.list_arrows, R.id.personal_value }); // 处理simpleAdapter中包括bitmap类型 adapter.setViewBinder(new ViewBinder() { public boolean setViewValue(View view, Object data, String textRepresentation) { if (view instanceof ImageView && data instanceof Bitmap) { ImageView image = (ImageView) view; image.setImageBitmap((Bitmap) data); return true; } return false; } }); mainListView.setAdapter(adapter);
private List<Map<String, Object>> GetPersonalData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("label", this.getResources().getString(R.string.personal_headphoto)); map.put("image", preferences.getString("photo", null) == null ? R.drawable.head : this.StringToBitMap(preferences.getString("photo", null)));//经过上述view.setViewBing之后在put值的时候就可以直接加入BitMap类型图片进行显示了 map.put("arrows", R.drawable.arrows_right); list.add(map); map = new HashMap<String, Object>(); map.put("label", this.getResources().getString(R.string.personal_name)); map.put("value", "APP测试"); list.add(map); map = new HashMap<String, Object>(); map.put("label", this.getResources().getString(R.string.personal_username)); map.put("value", "xxx"); list.add(map); map = new HashMap<String, Object>(); map.put("label", this.getResources().getString(R.string.personal_disease)); map.put("value", preferences.getString("disease", "无")); map.put("arrows", R.drawable.arrows_right); list.add(map); return list; }
注:着色部分意思为图片资源我用的base64所以这里边我转化了一下。