快速开发--adapter

由于公司项目需要,编写一款即时通讯软件,需要大量adapter。很多重复开发非常之繁琐。对此本人写了一个公共adapter。实际使用只需实现抽象方法即可。

个人编程思路尽量把重复代码提取出来,进一步封装,以功能类为单位,通过简单的抽象方法,继承实现具体方法。下面贴出源代码

 



/**
 * 公共适配器,只需重写抽象方法即可
 * 
 * @author buisthere
 * 
 * @param <T>
 */
public abstract class ArrayListAdapter<T> extends BaseAdapter {


protected ArrayList<T> mList;// 泛型list
protected Context mContext;// 没啥解释的 就是她


// protected ListView mListView;


public ArrayListAdapter(Context context) {
this.mContext = context;
}


/**
 * 添加list,当然是第一位
 * 
 * @param list
 */
public void addFirst(ArrayList<T> list) {
ArrayList<T> l = new ArrayList<T>();
l.addAll(list);
l.addAll(mList);
this.mList.clear();
this.mList = l;
notifyDataSetChanged();
}


/**
 * 如名
 */
public boolean isEmpty() {
if (mList != null && mList.size() == 0) {
return true;
} else {
return false;
}
}


/**
 * 获取数量
 */
public int getCount() {


return mList == null ? 0 : mList.size();
}


/**
 * 获取每个item
 */
public Object getItem(int position) {
if (position >= getCount())
return null;
return mList == null ? null : mList.get(position);
}


/**
 * 获取item的position
 */
public long getItemId(int position) {
return position;
}


/**
 * 根据position 移除item
 */
public void removeItem(int position) {
mList.remove(position);
this.notifyDataSetChanged();
}


/**
 * 抽象方法,
 */
abstract public View getView(int position, View convertView,
ViewGroup parent);


/**
 * 直接增加到现有list
 * 
 * @param list
 */
public void addLists(ArrayList<T> list) {
if (this.mList == null) {
this.mList = list;
} else {
this.mList.addAll(list);
}
notifyDataSetChanged();
}


/**
 * set方法
 */
public void setList(ArrayList<T> list) {
this.mList = list;
notifyDataSetChanged();
}


/**
 * 获取当前list
 * 
 * @return
 */
public ArrayList<T> getList() {
return mList;
}


/**
 * clear当前list
 */
public void clear() {
if (mList != null)
mList.clear();
notifyDataSetChanged();
}


/**
 * T[]添加到list集合
 * 
 * @param list
 */
public void setList(T[] list) {
ArrayList<T> arrayList = new ArrayList<T>(list.length);
for (T t : list) {
arrayList.add(t);
}
setList(arrayList);
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值