The design of SimpleAdapter is not good in my opinion.

An adapter should just adapter the data to view, not care to inflate xml to create Layout View, hold the member Views of the layout view, and, fill the datas to each member View one by one. SimpleAdapter does these things all together. It is high coupling design.

The solution is a ItemViewHolder class, which inflate and hold the layout view and its member views, and parse datas to fill them to member views one by one. And the most cool is, it’s layout view of ItemViewHolder set “this” as its tag. So that you can get the ItemViewHolder instance of the convertView in getView(…, View convertView, …) function.

See below code snippets.

In Adapter, passed in a List as data list.

1
List<Data> dataList;

and override getView() like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemViewHolder item = null; if(convertView == null) {
item = new ItemViewHolder(context);
convertView = item.getLayoutView();
} else {
item = (ItemViewHolder)convertView.getTag();
} item.setItemData(dataList.get(position)); return convertView;
}

ItemViewHolder is the views holder of the convertView.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class ItemViewHolder {  

    //The layout View of the item of the ListView.
private View layoutView = null;
//The member Views to display data.
private TextView textView = null;
private ImageView imageView = null;
… … public ItemViewHolder (Context context) {
super(context); initUI();
} public View getLayoutView() {
return layoutView;
} public void setItemData(Data data) {
textView.setText(data.getText());
imageView.setImage(data.getImage());
… …
} private void initUI() {
LayoutInflater inflater = LayoutInflater.from(mContext);
layoutView = inflater.inflate(R.layout.item_view_layout, null);
//
textView = (TextView) layoutView.findViewById(R.id.textview);
imageView = (ImageView)layoutView.findViewById(R.id.imageview);
//This is the most important code.
layoutView.setTag(this);
}
}

最新文章

  1. 用drawRect的方式实现一个尺子
  2. 整数转IP地址
  3. js-JavaScript高级程序设计学习笔记10
  4. 使用Navicat导入导出表的数据做测试(转载)
  5. CodeIgniter框架下载辅助函数的一个小bug
  6. 跨平台C的IDE
  7. MFC之向导页、消息框、文件选择、字体、颜色(三)
  8. sql xml 入门
  9. jmeter随笔(9)--有两种编码风格,导致数据乱码
  10. 织梦dedecms后台添加图片style全部都变成st&lt;x&gt;yle的解决办法
  11. Access数据库在线压缩的实现方法
  12. 关于js 构造 onclick 方法中传递Guid参数问题
  13. Unity3D-Shader-人物残影效果
  14. iOS 信号量
  15. 【动态规划】洛谷P1006传纸条
  16. Go Web --- 创建一个Article的增删改查
  17. 安装confluence5.10.0版本
  18. Javascript 日期格式化 相关操作
  19. JS - caller,callee,call,apply
  20. poj1860 Currency Exchange(spfa判断正环)

热门文章

  1. FBX
  2. ecshop 的transport.js 与jqueyr冲突
  3. Intent启动一个新的页面
  4. python中单引号,双引号,多引号区别
  5. Python之路【第十四篇】前端补充回顾
  6. MYSQL双主故障解决实例。
  7. servlet request getHeader(“x-forwarded-for”) 获取真实IP
  8. org.apache.commons.lang.StringUtils中常用的方法
  9. Swing杂记——Swing中引入Android的NinePatch技术,让Swing拥有Android的外观定制能力
  10. linux下的chm阅读器?