快速开发--adapter

来源:互联网 发布:淘宝达人怎么同步微淘 编辑:程序博客网 时间:2024/06/10 02:43

由于公司项目需要,编写一款即时通讯软件,需要大量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
原创粉丝点击