BaseAdapter的抽取
为了更方便高效的使用BaseAdapter,特意抽取了一下,下面是简单的结构图:

需要4个类:
【MyBaseAdapter3】:
public abstract class MyBaseAdapter3<T> extends BaseAdapter {
public List<T> list;
public MyBaseAdapter3(List<T> list) {
this.list = list;
}
@Override
public int getCount() {
return list==null?0:list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
T t=list.get(position);
BaseHolder holder;
if(convertView==null){
holder=getHolder();
}else{
holder= (BaseHolder) convertView.getTag();
}
//绑定数据
holder.setData(t);
return holder.getRootView();
}
public abstract BaseHolder getHolder();
}
【MyAdapter3】:
public class MyAdapter3 extends MyBaseAdapter3<Product>{
public MyAdapter3(List<Product> list) {
super(list);
}
@Override
public BaseHolder getHolder() {
return new MyHolder();
}
}
【BaseHolder】:
public abstract class BaseHolder<T> {
private View rootView;//每个item
private T t;//每个item的数据
public BaseHolder() {
rootView=initView();
rootView.setTag(this);
ButterKnife.bind(this,rootView);
}
public void setData(T t){
this.t=t;
refreshData(t);
}
public View getRootView() {
return rootView;
}
public abstract View initView();
protected abstract void refreshData(T t);
}
【MyHolder】:
public class MyHolder extends BaseHolder<Product> {
@Bind(R.id.p_name)
TextView pName;
@Bind(R.id.p_money)
TextView pMoney;
@Bind(R.id.p_yearlv)
TextView pYearlv;
@Bind(R.id.p_suodingdays)
TextView pSuodingdays;
@Bind(R.id.p_minzouzi)
TextView pMinzouzi;
@Bind(R.id.p_minnum)
TextView pMinnum;
@Bind(R.id.p_progresss)
RoundProgress2 pProgresss;
@Override
public View initView() {
return UIUtils.getXmlView(R.layout.item_product_list);
//一般将MyHolder写在MyAdapter3里变成内部类,这样上下文就可以使用Activity了
}
@Override
protected void refreshData(Product product) {
pMinnum.setText(product.getMemberNum());
pMinzouzi.setText(product.getMinTouMoney());
pMoney.setText(product.getMoney());
pName.setText(product.getName());
pSuodingdays.setText(product.getSuodingDays());
pYearlv.setText(product.getYearLv());
pProgresss.setProgress(Integer.parseInt(product.getProgress()));
}
}
【说明1】:一般将MyHolder写成适配器的内部类,这样上下文就可以使用Activity,使用Application作为上下文还有有些样式问题的隐患。
【说明2】:MyHolder用到了工具类UIUtils的下面一个方法,MyApplication extends Application
public static View getXmlView(int layoutId){
return View.inflate(MyApplication.mContext, layoutId, null);
}
BaseAdapter的抽取的更多相关文章
- Android BaseAdapter的使用
数据适配器有很多种,今天在这里记录一下最通用是适配器BaseAdapter. 首先说一下什么是适配器,这里我从网上找到一幅图片 由上图我们不难看出,所谓的适配器,就是数据与视图之间的桥梁.由它把数据绑 ...
- BaseAdapter优化深入分析
BaseAdapter是一个数据适配器,将我们提供的数据格式化为ListView可以显示的数据,BaseAdapter的优化直接影响到ListView的显示效率. 我们都知道,ListView自带有回 ...
- 9.代码抽取(adapter)
1 抽取Adapter 共性的方法 2 把getView方法里 和holder相关的逻辑 摘取到Holder代码中 3 把Holder 相关的代码 抽取到BaseHolder中 4 把ada ...
- 使用rowid抽取数据方法以及大数据量游标卡住的应对
平时工作的时候,经常会遇到这种事情,从一个大表A中,抽取字段a在一个相对较小B的表的数据,比如,从一个详单表中,抽取几万个用户号码的话单出来.这种时候,一般来说, 做关联查询: create tabl ...
- TODO:从数据库中随机抽取一条记录
TODO:从数据库中随机抽取一条记录 1.最直接,最粗暴的方法先计算记录的总数,然后选择一个从0到记录总数之间的随机数n,利用skip跳过n条记录,这是效率低下的的方法,首先的记录总数,在用skip会 ...
- Android笔记——BaseAdapter的使用
Android中的适配器(Adapter)是数据与视图(View)之间的桥梁,用于对要显示的数据进行处理,并通过绑定到组件进行数据的显示. BaseAdapter是Android应用程序中经常用到的基 ...
- 【NLP】Tika 文本预处理:抽取各种格式文件内容
Tika常见格式文件抽取内容并做预处理 作者 白宁超 2016年3月30日18:57:08 摘要:本文主要针对自然语言处理(NLP)过程中,重要基础部分抽取文本内容的预处理.首先我们要意识到预处理的重 ...
- SQL Server 随机数,随机区间,随机抽取数据rand(),floor(),ceiling(),round(),newid()函数等
在查询分析器中执行:select rand(),可以看到结果会是类似于这样的随机小数:0.36361513486289558,像这样的小数在实际应用中用得不多,一般要取随机数都会取随机整数.那就看下面 ...
- Android关于listView的BaseAdapter以及getView的三级优化
1.4个重写方法的含义 自定义Adapter继承自BaseAdapter(通用适配器) getCount(); getItem(); getItemId(); getViewTypaCount() ...
随机推荐
- 使用WCF的Trace与Message Log功能
原创地址:http://www.cnblogs.com/jfzhu/p/4030008.html 转载请注明出处 前面介绍过如何创建一个WCF Service http://www.cnblo ...
- C语言 · 查找整数
问题描述 给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个. 输入格式 第一行包含一个整数n. 第二行包含n个非负整数,为给定的数列,数列中的每个数都不大于10000. 第三行包含一个 ...
- C语言-指针
C指针基础知识 C语言中,指针无疑是最令人头疼的.今天无事就来学学C语言的指针,在此留下点笔记,仅供个人参考. 首先要搞懂的是,指针是什么? 指针:是用来存放内存地址的变量. 不管是什么类型的指针,存 ...
- OPEN CASCADE BSpline Curve Interpolation
OPEN CASCADE BSpline Curve Interpolation eryar@163.com Abstract. Global curve interpolation to point ...
- Visualize Surface by Delaunay Triangulator
Visualize Surface by Delaunay Triangulator eryar@163.com Abstract. Delaunay Triangulation is the cor ...
- OpenCascade Primitives BRep-Cone
OpenCascade Primitives BRep-Cone eryar@163.com Abstract. BRep is short for Boundary Representation. ...
- 深入理解this机制系列第二篇——this绑定优先级
前面的话 上一篇介绍过this的绑定规则,那如果在函数的调用位置上同时存在两种以上的绑定规则应该怎么办呢?本文将介绍this绑定的优先级 显式绑定 pk 隐式绑定 显式绑定胜出 function fo ...
- JavaScript之糟粕
0.导言 在上篇<JavaScript之毒瘤>中,列举了一些在JavaScript中难以避免的问题特性.本篇将会展示JavaScript中有问题的特性,但我们很容易就能便面它们.通过这些简 ...
- 安装Ubuntu时分区选择
最近购买来一台二手笔记本.型号是:Dell Latitude D520.回来之后就装上来Ubuntu12.04,开始是安装的UbuntuKylin 13.04.不知道是机器配置不行,还是本身系统有点卡 ...
- ECMAScript5之Object
在ECMAScript5中对Object新增的些方法,以前没注意的同志们,嘻嘻,下面我们再一起来边看边学. 1.Object之create Create单词意为创造嘛,作为Object的静态方法,不言 ...