ListView使用SimpleAdapter加载非drawable资源图片
来源:互联网 发布:天津平面设计美工招聘 编辑:程序博客网 时间:2024/05/18 15:53
ListView是一种常见的布局,通过SimpleAdapter加载数据既省力又美观,常用做法如下:
- SimpleAdapter mySimpleAdapter = new SimpleAdapter (
- Context context,
- List<? extends Map<String, ?>> data,
- int resource,
- String[] from,
- int[] to)
- myListview.setAdapter(mySimpleAdapter)
其中Context为ListView的activity
data: 为加载的数据
resource: 为ListView中加载的布局文件
from: 为data中的map的key值,与to中一一对应
to: 表示resource中对应的布局文件中空间的id,通常为R.id.xxxx
这样的做法在实际运行中有一个问题,如果map中包含有图片,而这个图片不是在drawable中存在的,比如网络图片,simpleAdapter本身是不支持的,网上搜索了很多做法,有的是自己做一个Adapter然后继承SimpleAdapter ,自定义,我查阅文档发现了一个ViewBinder,该类可以帮助SimpleAdapter加载图片。
- SimpleAdapter mySimpleAdapter = new SimpleAdapter (
- Context context,
- List<? extends Map<String, ?>> data,
- int resource,
- String[] from,
- int[] to);
- myListview.setAdapter(mySimpleAdapter)
- mySimpleAdapter.setViewBinder(new ViewBinder() {
- public boolean setViewValue(
- View view,
- Object data,
- String textRepresentation) {
- //判断是否为我们要处理的对象
- if(view instanceof ImageView && data instanceof Bitmap){
- ImageView iv = (ImageView) view;
- iv.setImageBitmap((Bitmap) data);
- return true;
- }else
- return false;
- }
- });
ViewBinder是一个接口,判断传进来的Data,通常是Object类型,然后将其转换成为Bitmap,这样我觉得多此一举,为什么对于String类型的数据可以自动转换,而Bitmap就不能转换呢,而且程序在运行期其实可以判断View是什么类型的View,完全可以在SimpleAdapter中封装好该段代码,也许还有其他用法吧。
- ListView使用SimpleAdapter加载非drawable资源图片
- ListView使用SimpleAdapter加载非drawable资源图片
- ListView使用SimpleAdapter加载网络图片
- 使用SimpleAdapter加载sdcard图片
- ListView使用SimpleAadapter加载drawable类型图片对象
- Android使用SimpleAdapter更新ListView里面的Drawable元素
- Android使用SimpleAdapter更新ListView里面的Drawable元素
- Android使用SimpleAdapter更新ListView里面的Drawable元素
- Android使用SimpleAdapter更新ListView里面的Drawable元素
- listview使用SimpleAdapter显示图片和文字
- Android--Gridview使用SimpleAdapter加载bitmap图片
- Drawable资源----图片资源
- 使用SimpleAdapter创建ListView
- Android ListView使用SimpleAdapter
- ListView-使用SimpleAdapter创建
- 使用SimpleAdapter填充ListView
- 使用SimpleAdapter创建ListView
- ListView -- SimpleAdapter的使用
- Linux如何配置ftp服务器、给ftp服务器添加访问用户
- android ListView显示网络图片
- Cannot recover key
- 2012 年度最受欢迎的中国开源软件评选结果
- perl学习网址
- ListView使用SimpleAdapter加载非drawable资源图片
- Three20研究院之完全自定义TabBar(八)
- android BaseActivity$1.onClick 匿名内部类 this.getClass()问题
- MySQL 常用指令
- Balsamiq Mockups简单介绍(UI草图绘制工具)
- 安装mysql 5.5 之后启动 mysql 5.5 command line client时 窗口一闪而过 怎么解决
- (转)在Servlet(或者Filter,或者Listener)中使用spring的IOC容器
- Mega Man‘s Missions UVA11795
- js跳转页面方法