在学习任何类之前,都非常有必要清楚它的体系图,这样才能够灵活运用。对程序的理解也是有极大的帮助的,废话不说,先上图
从上图不难看出AdapterView继承ViewGroup,所以AdapterView的本质也是容器,下面有三个直接抽象子类,要想使用需要找其子类,这就就看到经常使用的ListView。
ListView填充数据使用adapter,其实这是MVC设计模式这里简单说一下,MVC就是数据和视图完全分离,降低耦合性,所以给ListView填充数据的时候才需要使用adapter。
下面是Adapter关系图:
adapter有很多种,常用的有四种
ArrayAdapter:简单易用,一般将数组或者List集合多个值包装成多个列表项。但列表项只能是TextView
SimpleAdapter:名字叫简单适配器,但它的功能很强大,唯一构造方法,它有五个参数
参数1:上下文,基本上所有组件都需要
参数2:List集合,集合里的每一个元素是map集合。
参数3:设置Layout的XML文件(XML里设置每一项item显示的视图)
参数4:从这map提取那些Key值生成对应的Value(String [] )
参数5:参数4取完的Value放在那些控件中(int [])
SimpleCursorAdapter:跟SimpleAdapter类似,只是用于包装Cursor数据。
BaseAdapter:自定义adapter继承它,实现功能的扩展
注意:Adapter和AdapterVIew容易混淆,Adapter是MVC设计模式中的中的C也是接口,用于控制数据显示,AdapterView负责合适的方式显示,主要是一些监听的设置。
下面看一下ListView和adapter结合使用的
MainActivity.java
public class MainActivity extends Activity{
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义两个数组用于给TextView显示
String name [] = {"赵红兵","刘铁柱","小北京","王岳"};
String descs [] = new String []{"当过兵的小子","修自行车的大哥","能打能说相声","戴眼镜的真汉子"};
list = (ListView) findViewById(R.id.list);
//循环给List集合中的每一项添加Map数据
for(int i = 0 ;i<name.length;i++){
Map<String,Object> map = new HashMap<String, Object>();
map.put("name", name[i]);
map.put("descs", descs[i]);
map.put("img", android.R.drawable.ic_dialog_dialer);
data.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_list, new String[]{"name","descs","img"}, new int[]{R.id.tv1,R.id.tv2,R.id.img});
//给lListView设置适配器
list.setAdapter(adapter);
//设置ListView选项的点击事件,该事件源自于AdapterView中的事件。
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//当ListView被点击调用该方法
Toast.makeText(MainActivity.this, position+"被点击了", 0).show();
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!-- 这三个控件共同拼接成ListView的一个Item -->
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
效果图:
当ListVIew的高度不是match_parent的时候适配器的getView会调用三次,具体原因未知。
这里也使用了ListView监听,监听的实质是接口回调,根据java多肽性调用子类重写之后的方法,通过参数传递数据。
另外ListView通过XML数组也可以设置的,缺点不灵活,数据不易修改,不推荐使用。