个人总结android中的BaseAdapter

本章不讨论Android中的BaseAdapter的工作原理,只是将其封装成一个通用的Adapter。在Android5.0有了RecyclerView的封装好的RecyclerView.Adapter,但是如果你的版本过低可能会不兼容,所以就封装了一下,希望能给阅读者帮助,如果有不当之处,望能指点更正。


public  abstract class ParentAdapter<T> extends BaseAdapter {


/**
* 集合
*/
protected List<T> mLists = null;


/**
* 数组
*/
protected T[] mArays;


/**
* 上下文
*/
protected Context mContext;


/**
* 填充布局文件的类
*/
protected LayoutInflater mInflater;


/**
* 只适用于填充数据集合 : List<T>

* @param ctx
*/
public ParentAdapter(Context ctx) {
mLists = new ArrayList<T>();
this.mContext = ctx;
this.mInflater = LayoutInflater.from(ctx);
}


/**
* 构造函数

* @param ctx
*            上下文
* @param lists
*            集合
*/
public ParentAdapter(Context ctx, List<T> lists) {
this.mLists = lists;
if (null == lists) {
mLists = new ArrayList<T>();
}
this.mContext = ctx;
this.mInflater = LayoutInflater.from(ctx);
}


/**
* 构造函数

* @param ctx
*            上下文
* @param mArays
*            数组
* @throws Exception
*             检查数组是否为空
*/
public ParentAdapter(Context ctx, T[] mArays) throws Exception {
if (null == mArays) {
throw new Exception("mArays not null !!!");
}
this.mArays = mArays;
this.mContext = ctx;
this.mInflater = LayoutInflater.from(ctx);
}


@Override
public int getCount() {
if (null != mLists) {
return mLists.size();
} else if (null != mArays) {
return mArays.length;
}
return 0;
}


@Override
public T getItem(int position) {
if (null != mLists) {
return mLists.get(position);
} else if (null != mArays) {
return mArays[position];
}
return null;
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}


@Override
public abstract View getView(int position, View convertView, ViewGroup parent);


/**
* 设置数据,以前数据会清空

* @param collections
*/
public boolean setDatas(Collection<T> collections) {
try {
if (null == mLists) {
return false;
}
mLists.clear();
if (null != collections && 0 != collections.size()) {
mLists.addAll(collections);
}
notifyDataSetChanged();
return true;
} catch (Exception e) {
e.printStackTrace();

}
return false;
}


/**
* 在原始数据上添加新数据 添加单项

* @param t
*/
public boolean addData(T t) {
try {
if (null == mLists) {
return false;
}
if (null != t) {
mLists.add(t);
notifyDataSetChanged();
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}


/**
* 在原始数据上添加新数据 <功能详细描述> 添加集合

* @param collections
*/
public boolean addData(Collection<T> collections) {
try {
if (null == mLists) {
return false;
}
if (null != collections && 0 != collections.size()) {
mLists.addAll(collections);
notifyDataSetChanged();
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}


/**
* 清除所有集合元素
*/
public boolean clearAllData() {
try {
if (null == mLists) {
return false;
}
if (0 == mLists.size()) {
return true;
}
mLists.clear();
notifyDataSetChanged();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}


/**
* 获取mLists或者mArrays的总的行数

* @return
*/
public int getTotalSize() {
if (null != mLists) {
return mLists.size();
} else if (null != mArays) {
return mArays.length;
}
return 0;
}


/**
* 获取mLists

* @return
*/
public List<T> getMLists() {
return mLists;
}


/**
* 获取mArays

* @return
*/
public T[] getMArays() {
return mArays;
}


/**
* 移除单独的一个元素

* @param position
*/
public boolean removeData(int position) {
try {
if (null == mLists) {
return false;
}
if (position < getTotalSize()) {
mLists.remove(position);
notifyDataSetChanged();
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}


/**
* 获取mLists或者mArrays其中位置的数据

* @param index
*            所在的集合或者数组中的位置,不是在Adapter中的位置
* @return
*/
public T getData(int index) {
if (index < getTotalSize()) {
try {
if (null != mLists) {
return mLists.get(index);
} else if (null != mArays) {
return mArays[index];
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}


/**
* 更新局部信息

* @param position
* @param t
* @return
*/
public boolean updateData(int position, T t) {
try {
if (position >= 0 && position < getTotalSize()) {
mLists.set(position, t);
notifyDataSetChanged();
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}


}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值