The Adapter of ListView: Just adapt data to view, don’t do anything else
2016-03-17 19:26
411 查看
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.
and override getView() like this:
ItemViewHolder is the views holder of the convertView.
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; |
12 | @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; } |
12 | 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); } } |
相关文章推荐
- IOS控件学习之UIScrollView(3)
- c语言:通过指向结构体变量的指针变量输出结构体变量中成员的信息
- 多重循环
- 异步消息处理机制 Handler Looper Message
- Linux Recover MYSQL Password
- 同一台服务器运行多套kamailio服务
- HDU 1005 Number Sequence
- Shape + Selector: Make a Shape as one item of the Selector
- 多线程--创建线程两种方式实例
- 模板:树状数组
- eclipse里自动生成getset方法
- ajax跨域post请求的java代理实现
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- html日记——三栏式布局
- LCD控制器的数据格式
- Linux下软链接与硬链接的区别
- stmmac ethernet
- 在asp.net工程中使用jQuery-ui的autocomplete功能
- zabbix 二次开发之同步CMDB主机和模板绑定
- zabbix 二次开发之同步CMDB主机和模板绑定