android中的listView
2018-10-28 本文已影响0人
沈默的头号狗腿
在安卓开发中,listView是常用的组件之一。
使用listView最关键的就是如何去选择或者设计他的Adapter
androidSDK自带的Adapter有很多种,常用的有以下几种

接下来我们来看看几种Adapter的使用方式
arrayAdapter,SimpleAdapter以及SimpleCursorAdapter(BaseAdapter是所有Adapter的父类)
以上三种adapter为SDK自带的三个Adapter

如图可以看到,ArrayAdapter需要的数据类型为数组类型,以下是ArrayAdapter使用实例,这个适配器只能显示数据


SimpleAdapter所需要的数据类型为list类型,以下是SimpleAdapter使用实例


效果图

从上述使用效果来看,SimpleAdapter需要list类型的数据集,其中list的每个元素为map用以存放每行的多个显示项,并且可以看到,此时自定义的xml文件可以定义布局为根元素,且点击事件
能够生效。
SimpleCursorAdapter目前暂时未使用到,以后再作补充
自定义Adapter
自定义Adapter一般通过继承BaseAdapter来实现,在BaseAdapter中重写几个方法即可完成
如下图

以上就是几种Adapter的使用方式。