个人总结android中的BaseAdapter
来源:互联网 发布:windows属性 编辑:程序博客网 时间:2024/05/06 05:24
本章不讨论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;
}
}
- 个人总结android中的BaseAdapter
- Android BaseAdapter使用总结
- 关于Android中的BaseAdapter
- Android中的BaseAdapter使用说明
- Android Baseadapter 中的convertView
- Android 重写BaseAdapter中的getView
- Android中的BaseAdapter(二)
- Android中ArrayAdapter、SimpleAdapter、BaseAdapter总结
- Android知识总结--ListView的BaseAdapter抽取
- Android中的基础适配器——BaseAdapter
- 关于Android 中的BaseAdapter用法理解
- Android中的BaseAdapter的使用极其优化
- android中的适配器(ArrayAdapter,SimpleAdapter,BaseAdapter)
- Android --- BaseAdapter
- Android BaseAdapter
- android BaseAdapter
- Android BaseAdapter
- Android BaseAdapter
- Linux网络编程
- Android Studio的Git与Github配置
- geoip_api_php 通过ip找到国家的库包
- Qt移植到ARM Linux教程--转载
- leetcode第24题——**Swap Nodes in Pairs
- 个人总结android中的BaseAdapter
- Java-Lambda
- [java]7、测试substring
- 004.设计模式之建造者模式
- Android Studio 导入zxing项目
- android 设置闹钟播放声音
- ListView中Title和content 布局,两个都显示就平分,显示一个就居中
- MAC上搭建lua开发环境
- Win7 语言包 32/64 bit