Android BaseAdapter
2013-10-05 11:40
344 查看
在Android应用开发中,列表显示是经常会碰到的功能。开发列表显示功能离不开Adapter类。一般比较简单的列表功能使用SimpleAdapter就能完成了。比较复杂的话,就需要自定义Adapter类,在自定义的Adapter类中要具体实现BaseAdapter类中的一些方法。
1.自定义Adapter类的成员
一般来说,自定义的Adapter类必要的成员有:
LayoutInflater对象,作为用来获取、设置一个ListView项的UI的桥梁;
(构造函数中:inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
getView方法中:
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView text = (TextView) convertView.findViewById(R.id.list_item_text);
)
List<data>对象,保存需要呈现的数据。
2.自定义Adapter类的构造函数:
public ListViewAdapter(Context context,List<String> items){
if(items==null){
items=new ArrayList<String>();
}else{
this.items=items;
}
inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
3.公共方法:
//获取数据项数
public int getCount(){
}
//获取数据集合中某一位置的数据对象
public Object getItem(int position) {
}
//设置一个数据项的UI呈现形式和显示内容
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView text = (TextView) convertView.findViewById(R.id.list_item_text);
//下面就可以设置ListView中每一项的内容和UI了
}
使用SimpleAdapter:
一条原始数据使用HashMap保存,整体则是一个ArrayList<HashMap>。
ListView.setAdapter(new SimpleAdapter(Context, HashMap列表, 数据项的layout, new String[]{HashMap中的键列表}, new int[]{数据项的layout中用来显示数据的控件id列表}));
1.自定义Adapter类的成员
一般来说,自定义的Adapter类必要的成员有:
LayoutInflater对象,作为用来获取、设置一个ListView项的UI的桥梁;
(构造函数中:inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
getView方法中:
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView text = (TextView) convertView.findViewById(R.id.list_item_text);
)
List<data>对象,保存需要呈现的数据。
2.自定义Adapter类的构造函数:
public ListViewAdapter(Context context,List<String> items){
if(items==null){
items=new ArrayList<String>();
}else{
this.items=items;
}
inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
3.公共方法:
//获取数据项数
public int getCount(){
}
//获取数据集合中某一位置的数据对象
public Object getItem(int position) {
}
//设置一个数据项的UI呈现形式和显示内容
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView text = (TextView) convertView.findViewById(R.id.list_item_text);
//下面就可以设置ListView中每一项的内容和UI了
}
使用SimpleAdapter:
一条原始数据使用HashMap保存,整体则是一个ArrayList<HashMap>。
ListView.setAdapter(new SimpleAdapter(Context, HashMap列表, 数据项的layout, new String[]{HashMap中的键列表}, new int[]{数据项的layout中用来显示数据的控件id列表}));
相关文章推荐
- Android———从GitHub上下载源码的方法【Written By KillerLegend】
- Android Studio设置主题 和 不支持中文的问题解决方法
- Android获取SD卡总容量,可用大小,机身内存总容量及可用大小的系统方法
- Android打开多个Activity后返回到第一个Activity
- Android学习笔记AutoCompleteTextView的使用
- Android获取SD卡总容量,可用大小,机身内存总容量及可用大小的系统方法
- Android获取SD卡总容量,可用大小,机身内存总容量及可用大小的系统方法
- android客户端给服务端发post请求
- android studio中文社区
- Android Studio使用教程
- 查看android证书签名
- Mac下Android Studio简单使用
- DEVDIV原创ANDROID学习系列教程实例
- Android Studio开发初级教程
- Android Studio工程列表没有文件夹,如何恢复默认设置
- android studio设置
- android环境搭建老出现fetch error的解决办法
- 【绝对给力】Android开发免豆资料(教程+工具+源码)地址汇总
- 【绝对给力】Android开发免豆资料(教程+工具+源码)地址汇总
- 【绝对给力】Android开发免豆资料(教程+工具+源码)地址汇总