Android之Adapter用法总结.docx
《Android之Adapter用法总结.docx》由会员分享,可在线阅读,更多相关《Android之Adapter用法总结.docx(16页珍藏版)》请在冰豆网上搜索。
Android之Adapter用法总结
Android之Adapter用法总结
1.概念
Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带。
在常见的View(ListView,GridView)等地方都需要用到Adapter。
如下图直观的表达了Data、Adapter、View三者的关系:
Android中所有的Adapter一览:
由图可以看到在Android中与Adapter有关的所有接口、类的完整层级图。
在我们使用过程中可以根据自己的需求实现接口或者继承类进行一定的扩展。
比较常用的有BaseAdapter,SimpleAdapter,ArrayAdapter,SimpleCursorAdapter等。
∙BaseAdapter是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性;
∙ArrayAdapter支持泛型操作,最为简单,只能展示一行字。
∙SimpleAdapter有最好的扩充性,可以自定义出各种效果。
∙SimpleCursorAdapter可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。
如需要实现更复杂的UI也可以重写其他方法。
可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。
2.应用案例
1)ArrayAdapter
列表的显示需要三个元素:
a.ListVeiw用来展示列表的View。
b.适配器用来把数据映射到ListView上的中介。
c.数据具体的将被映射的字符串,图片,或者基本组件。
案例一
publicclassArrayAdapterActivityextendsListActivity{
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
//列表项的数据String[]strs={"1","2","3","4","5"};
ArrayAdapteradapter=newArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,strs);
setListAdapter(adapter);
}
}
案例二
publicclassMyListViewextendsActivity{
privateListViewlistView;
//privateListdata=newArrayList();@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
listView=newListView(this);
listView.setAdapter(newArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,getData()));
setContentView(listView);
}
privateListgetData(){
Listdata=newArrayList();
data.add("测试数据1");
data.add("测试数据2");
data.add("测试数据3");
data.add("测试数据4");
returndata;
}
}
上面代码使用了ArrayAdapter(Contextcontext,inttextViewResourceId,Listobjects)来装配数据,要装配这些数据就需要一个连接ListView视图对象和数组数据的适配器来两者的适配工作,ArrayAdapter的构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。
同时用setAdapter()完成适配的最后工作。
效果图如下:
2)SimpleAdapter
simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。
下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。
案例一
simple.xml
xmlversion="1.0"encoding="utf-8"?
>
android="
android:
orientation="vertical"
android:
layout_width="fill_parent"
android:
layout_height="fill_parent">
id="@+id/img"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_margin="5dp"/>
id="@+id/title"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
textColor="#ffffff"
android:
textSize="20sp"/>
publicclassSimpleAdapterActivityextendsListActivity{
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
SimpleAdapteradapter=newSimpleAdapter(this,getData(),R.layout.simple,newString[]{"title","img"},newint[]{R.id.title,R.id.img});
setListAdapter(adapter);
}
privateList
//map.put(参数名字,参数值)List
Mapmap=newHashMap();
map.put("title","摩托罗拉");
map.put("img",R.drawable.icon);
list.add(map);
map=newHashMap();
map.put("title","诺基亚");
map.put("img",R.drawable.icon);
list.add(map);
map=newHashMap();
map.put("title","三星");
map.put("img",R.drawable.icon);
list.add(map);
returnlist;
}
}
案例二
下面的程序是实现一个带有图片的类表。
首先需要定义好一个用来显示每一个列内容的xml,vlist.xml
xmlversion="1.0"encoding="utf-8"?
>
android="android:
orientation="horizontal"android:
layout_width="fill_parent"
android:
layout_height="fill_parent">
id="@+id/img"android:
layout_width="wrap_content"android:
layout_height="wrap_content"android:
layout_margin="5px"/>
orientation="vertical"android:
layout_width="wrap_content"android:
layout_height="wrap_content">
id="@+id/title"android:
layout_width="wrap_content"android:
layout_height="wrap_content"
android:
textColor="#FFFFFFFF"android:
textSize="22px"/>
id="@+id/info"android:
layout_width="wrap_content"android:
layout_height="wrap_content"
android:
textColor="#FFFFFFFF"android:
textSize="13px"/>
publicclassMyListView3extendsListActivity{
//privateListdata=newArrayList();@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
SimpleAdapteradapter=newSimpleAdapter(this,getData(),R.layout.vlist,
newString[]{"title","info","img"},
newint[]{R.id.title,R.id.info,R.id.img});
setListAdapter(adapter);
}
privateList>getData(){
List>list=newArrayList>();
Mapmap=newHashMap();
map.put("title","G1");
map.put("info","google1");
map.put("img",R.drawable.i1);
list.add(map);
map=newHashMap();
map.put("title","G2");
map.put("info","google2");
map.put("img",R.drawable.i2);
list.add(map);
map=newHashMap();
map.put("title","G3");
map.put("info","google3");
map.put("img",R.drawable.i3);
list.add(map);
returnlist;
}
}
使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。
HashMap的每个键值数据映射到布局文件中对应id的组件上。
因为系统没有对应的布局文件可用,我们可以自己定义一个布局vlist.xml。
下面做适配,new一个SimpleAdapter参数一次是:
this,布局文件(vlist.xml),HashMap的title和info,img。
布局文件的组件id,title,info,img。
布局文件的各组件分别映射到HashMap的各元素上,完成适配。
运行效果如下图:
3)SimpleCursorAdapter
publicclassSimpleCursorAdapterActivityextendsListActivity{
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
//获得一个指向系统通讯录数据库的Cursor对象获得数据来源Cursorcur=getContentResolver().query(People.CONTENT_URI,null,null,null,null);
startManagingCursor(cur);
//实例化列表适配器
ListAdapteradapter=newSimpleCursorAdapter(this,android.R.layout.simple_list_item_1,cur,newString[]{People.NAME},newint[]{android.R.id.text1});
setListAdapter(adapter);
}
}
一定要以数据库作为数据源的时候,才能使用SimpleCursorAdapter,这里特别需要注意的一点是:
不要忘了在AndroidManifest.xml文件中加入权限
name="android.permission.READ_CONTACTS">
效果如下:
4)BaseAdapter
有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮。
添加按钮首先要写一个有按钮的xml文件,然后自然会想到用上面的方法定义一个适配器,然后将数据映射到布局文件上。
但是事实并非这样,因为按钮是无法映射的,即使你成功的用布局文件显示出了按钮也无法添加按钮的响应,这时就要研究一下ListView是如何现实的了,而且必须要重写一个类继承BaseAdapter。
下面的示例将显示一个按钮和一个图片,两行字如果单击按钮将删除此按钮的所在行。
并告诉你ListView究竟是如何工作的。
vlist2.xml
xmlversion="1.0"encoding="utf-8"?
>
android="android:
orientation="horizontal"android:
layout_width="fill_parent"
android:
layout_height="fill_parent">
id="@+id/img"android:
layout_width="wrap_content"android:
layout_height="wrap_content"android:
layout_margin="5px"/>
orientation="vertical"android:
layout_width="wrap_content"android:
layout_height="wrap_content">
id="@+id/title"android:
layout_width="wrap_content"android:
layout_height="wrap_content"
android:
textColor="#FFFFFFFF"android:
textSize="22px"/>
id="@+id/info"android:
layout_width="wrap_content"android:
layout_height="wrap_content"
android:
textColor="#FFFFFFFF"android:
textSize="13px"/>
id="@+id/view_btn"android:
layout_width="wrap_content"android:
layout_height="wrap_content"
android:
text="@string/s_view_btn"android:
layout_gravity="bottom|right"/>
/**
002*@author
003*
004*/
005publicclassMyListView4extendsListActivity{
006
007
008privateList>mData;
009
010@Override
011publicvoidonCreate(BundlesavedInstanceState){
012super.onCreate(savedInstanceState);
013mData=getData();
014MyAdapteradapter=newMyAdapter(this);
015setListAdapter(adapter);
016}
017
018privateList>getData(){
019List>list=newArrayList>();
020
021Mapmap=newHashMap();
022map.put("title","G1");
023map.put("info","google1");
024map.put("img",R.drawable.i1);
025list.add(map);
026
027map=newHashMap();
028map.put("title","G2");
029map.put("info","google2");
030map.put("img",R.drawable.i2);
031list.add(map);
032
033map=newHashMap();
034map.put("title","G3");
035map.put("info","google3");
036map.put("img",R.drawable.i3);
037list.add(map);
038
039returnlist;
040}
041
042//ListView中某项被选中后的逻辑043@Override
044protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){
045
046Log.v("MyListView4-click",(String)mData.get(position).get("title"));
047}
048
049/**
050*listview中点击按键弹出对话框
051*/
052publicvoidshowInfo(){
053newAlertDialog.Builder(this)
054.setTitle("我的listview")
055.setMessage("介绍...")
056.setPositiveButton("确定",newDialogInterface.OnClickListener(){
057@Override
058publicvoidonClick(DialogInterfacedialog,intwhich){
059}
060})
061.show();
062
063}
064
065
066
067publicfinalclassViewHolder{
068publicImageViewimg;
069publicTextViewtitle;
070publicTextViewinfo;
071publicButtonviewBtn;
072}
073
074
075publicclassMyAdapterextendsBaseAdapter{
076
077privateLayoutInflatermInflater;
078
079
080publicMyAdapter(Contextcontext){
081this.mInflater=LayoutInflater.from(context);
082}
083@Override
084publicintgetCount(){
085//TODOAuto-