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);
}
}

The Adapter of ListView: Just adapt data to view, don’t do anything else的更多相关文章

  1. 使用自定的Adapter绑定ListView/GridView数据

    使用自定的Adapter绑定ListView/GridView数据(地址) 对于ListView/Gridview的数据绑定, google提供了一些Adapter的模板, 自己也可以自定义一些个性化 ...

  2. 【转】Android自定义Adapter的ListView的思路及代码

    原文网址:http://www.jb51.net/article/37236.htm Android自定义Adapter的ListView的思路及代码,需要的朋友可以参考一下   在开发中,我们经常使 ...

  3. 【转载】逃离adapter的地狱-针对多个View type的组合实现方案

    英文原文:JOE'S GREAT ADAPTER HELL ESCAPE 转载地址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015 ...

  4. iphone dev 入门实例2:Pass Data Between View Controllers using segue

    Assigning View Controller Class In the first tutorial, we simply create a view controller that serve ...

  5. 关于自定义Adapter实现ListView的使用

    以下为使用BaseAdapter作扩展,自定义Adapter来使用ListView控件: 需要注意以下的几点: 1.自定义Adapter时,需要特别注意Adapter类中getView()方法覆盖,注 ...

  6. Android学习----自定义Adapter实现ListView

    前言: 对于ListView而言,自定义的Adapter对于显示复杂的界面有很大的灵活性 .使用自定义的Adapter需要继承BaseAdapter,然后重写getCount(),getView(), ...

  7. Android 自定义Adapter 但listview 只显示第一条数据

    <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content ...

  8. 【转】Android之自定义Adapter的ListView

    http://www.cnblogs.com/topcoderliu/archive/2011/05/07/2039862.html 在开发中,我们经常使用到ListView这个控件.Android的 ...

  9. Android之自定义Adapter的ListView

    ListView的创建,一般要具备两大元素: 1)数据集,即要映射的字符串.图片信息之类. 2)适配器,实现把要映射的字符串.图片信息映射成视图(如Textview.Image等组件),再添加到Lis ...

随机推荐

  1. TemplateDataField

    .aspx <ig:TemplateDataField Key="TemplateField_0"> <Header Text="selected&qu ...

  2. HTML5学习预览

    HTML5中,新增了很多input元素的类型 email         email类型用于应该包含 e-mail 地址的输入域. url         url 类型用于应该包含 URL 地址的输入 ...

  3. Java排序算法——插入排序

    import java.util.Arrays; //================================================= // File Name : Select_S ...

  4. 枚举类型Enum

    包java.dataStructure中,文件名Enum_demo.java 在JDK5中引入了一个新的关键字——enum,可以直接定义枚举类型 在申明枚举类的时候,也可以申明属性.方法和构造函数,但 ...

  5. ast模块

    有这么一个需求,你想从文件中读取字典,方法有很多,这里用的是ast模块 import ast with open("account","r",encoding= ...

  6. redis删除list中指定index的值

    Redis的List删除命令: lrem : lrem mylist 0 "value"    //从mylist中删除全部等值value的元素   0为全部,负值为从尾部开始. ...

  7. cookie和session的对比

    1.存放的位置     cookie存在客户端的临时文件夹     session:存在服务器的内存中,一个session域对象为一个用户浏览器服务. 2.安全性   cookie是以明文方式存放在客 ...

  8. 【8-15】Markdown语法学习

    学习Markdown语法 来源简书URL #,支持六级标题 列表 用-或*(指无序列表),有序列表直接1. 2. 3. 这样,中间有空格,可乱序(-+*都可,不能混合使用,混合使用为嵌套) 这是一个无 ...

  9. 黑客攻防技术宝典Web实战篇(一)Web应用程序技术基础

    在开展Web应用程序渗透测试之前请先了解下面列出的这些内容,如果不是很懂的话,请读David Gourley & Brian Totty的HTTP权威指南也叫HTTP:The Definiti ...

  10. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...