各种Adapter的用法.docx
- 文档编号:13691686
- 上传时间:2023-06-16
- 格式:DOCX
- 页数:16
- 大小:226.46KB
各种Adapter的用法.docx
《各种Adapter的用法.docx》由会员分享,可在线阅读,更多相关《各种Adapter的用法.docx(16页珍藏版)》请在冰点文库上搜索。
各种Adapter的用法
packagecom.shang.test;
2
3 importjava.util.ArrayList;
4
5 importandroid.app.Activity;
6 importandroid.os.Bundle;
7 importandroid.widget.ArrayAdapter;
8 importandroid.widget.ListView;
9
10 /**
11*
12*@authorshangzhenxiang
13*
14*/
15 publicclassTestArrayAdapterActivityextendsActivity{
16
17privateListViewmListView;
18privateArrayList
19
20@Override
21protectedvoidonCreate(BundlesavedInstanceState){
22super.onCreate(savedInstanceState);
23setContentView(R.layout.testarrayadapter);
24mListView=(ListView)findViewById(R.id.myArrayList);
25mListView.setAdapter(newArrayAdapter
26}
27
28privateArrayList
29mArrayList.add("测试数据1");
30mArrayList.add("测试数据2");
31mArrayList.add("测试数据3");
32mArrayList.add("测试数据4");
33mArrayList.add("测试数据5");
34mArrayList.add("测试数据6");
35returnmArrayList;
36}
37}
布局里面有个ListView就可以了:
1
xmlversion="1.0"encoding="utf-8"?
>
2 android=" 3android: orientation="vertical" 4android: layout_width="match_parent" 5android: layout_height="fill_parent"> 6 7android: layout_width="match_parent" 8android: layout_height="wrap_content" 9android: text="@string/hello"/> 10 11android: id="@+id/myArrayList" 12android: layout_width="match_parent" 13android: layout_height="wrap_content"/> 14 上面的代码中用到了ArrayAdapter的构造方法: publicArrayAdapter(Contextcontext,inttextViewResourceId,T[]objects) Api中是这么描述的: 其中Context为当前的环境变量,可以显示一行文字的一个布局文件,和一个List的集合,也就是数据源。 布局文件可以自己写,也可以用系统的,我这里是用的系统的。 自己写的布局中包含一个TextView就可以了,当然系统中也有包含一个TextView的布局文件: 就是android.R.layout.simple_expandable_list_item_1,调用这个比较方便。 这里给出运行后的效果图: 下面说说SimpleCursorAdapter: Api中是这么说的: AneasyadaptertomapcolumnsfromacursortoTextViewsorImageViewsdefinedinanXMLfile.Youcanspecifywhichcolumnsyouwant,whichviewsyouwanttodisplaythecolumns,andtheXMLfilethatdefinestheappearanceoftheseviews. 简单的说就是方便把Cursor中得到的数据进行列表显示,并可以把指定的列映射到指定的TextView上。 我这里写的是从联系人中拿到数据并显示在列表上。 代码如下: 1packagecom.shang.test; 2 3 importandroid.app.Activity; 4 importandroid.database.Cursor; 5 importandroid.os.Bundle; 6 importandroid.provider.Contacts.People; 7 importandroid.widget.ListView; 8 importandroid.widget.SimpleCursorAdapter; 9 10 /** 11* 12*@authorshangzhenxiang 13* 14*/ 15publicclassTestSimpleCursorAdapterextendsActivity{ 16 17privateListViewmListView; 18privateCursormCursor; 19privateSimpleCursorAdaptermAdapter; 20 21@Override 22protectedvoidonCreate(BundlesavedInstanceState){ 23super.onCreate(savedInstanceState); 24setContentView(R.layout.testsimplecursoradapter); 25mListView=(ListView)findViewById(R.id.mySimpleCursorList); 26mCursor=getContentResolver().query(People.CONTENT_URI,null,null,null,null); 27startManagingCursor(mCursor); 28mAdapter=newSimpleCursorAdapter(TestSimpleCursorAdapter.this,android.R.layout.simple_expandable_list_item_1,mCursor,newString[]{People.NAME},newint[]{android.R.id.text1}); 29mListView.setAdapter(mAdapter); 30} 31} mCursor=getContentResolver().query(People.CONTENT_URI,null,null,null,null);是先获得一个指向系统联系人的Cursor startManagingCursor(mCursor);是指我们把Cursor交给这个Activity保管,这样Cursor便会和Activity同步,我们不用手动管理了。 simpleCursorAdapterAPI中是这样说的: 其中前面的2个参数跟ArrayAdapter中是一样的,第三个参数是传个来的参数,其实也是数据源,后面的2个参数是2个数组,前一个是String【】类型的,而后一个是int【】类型的,说明前一个参数中的值对应的是从数据库中的字段,后一个是布局文件中和这个字段对应的id,也就是说这个字段对应得值要显示在哪里(比如说我们这里查到的联系人中的NAME字段,要显示在一个对应的TextView上面)。 这里我们具体看一下系统的布局,也就是我们这里的第二个参数的布局,便于理解,android.R.layout.simple_expandable_list_item_1.xml文件中是这么写的: 1 xmlversion="1.0"encoding="utf-8"? > 2 --Copyright(C)2006TheAndroidOpenSourceProject 3 4LicensedundertheApacheLicense,Version2.0(the"License"); 5youmaynotusethisfileexceptincompliancewiththeLicense. 6YoumayobtainacopyoftheLicenseat 7 8http: //www.apache.org/licenses/LICENSE-2.0 9 10Unlessrequiredbyapplicablelaworagreedtoinwriting,software 11distributedundertheLicenseisdistributedonan"ASIS"BASIS, 12WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. 13SeetheLicenseforthespecificlanguagegoverningpermissionsand 14limitationsundertheLicense. 15--> 16 17 android=" 18android: id="@android: id/text1" 19android: layout_width="match_parent" 20android: layout_height="? android: attr/listPreferredItemHeight" 21android: paddingLeft="? android: attr/expandableListPreferredItemPaddingLeft" 22android: textAppearance="? android: attr/textAppearanceLarge" 23android: gravity="center_vertical" 24/> 注意他有一个id,这个id也是系统的id,这个布局中只有一个TextView,所以只能显示一个字段,我们这里显示的联系人的名字, 而最后的一个参数就是由这么写id组成的一个数据(如果有很多TextView的话)。 比如说我们要显示很多字段,布局文件中就要写很多TextView,而每一个TextView都有一个ID,第三个参数中有多少个字段,第四个参数中就有多少个id,并一一对应。 我们来看一下运行效果图: 上面说到的2种方法都是显示的文字,比方说我们要显示图片怎么办呢,还要显示很多内容,还要按自己喜欢的布局排列怎么办呢,用SimpleAdapter,扩展性好,可以定义各种各样的布局。 代码如下: 1packagecom.shang.test; 2 3importjava.util.ArrayList; 4importjava.util.HashMap; 5importjava.util.List; 6 7importandroid.app.Activity; 8importandroid.os.Bundle; 9importandroid.widget.ListView; 10importandroid.widget.SimpleAdapter; 11 12/** 13* 14*@authorshangzhenxiang 15* 16*/ 17publicclassTestSimpleAdapterextendsActivity{ 18 19privateListViewmListView; 20privateSimpleAdaptermAdapter; 21privateList 22privateHashMap 23 24@Override 25protectedvoidonCreate(BundlesavedInstanceState){ 26super.onCreate(savedInstanceState); 27setContentView(R.layout.testsimpleadapter); 28mListView=(ListView)findViewById(R.id.mySimpleList); 29mAdapter=newSimpleAdapter(this,getData(),R.layout.simpleitem,newString[]{"image","title","info"},newint[]{R.id.img,R.id.title,R.id.info}); 30mListView.setAdapter(mAdapter); 31} 32 33privateList 34mHashMaps=newArrayList 35map=newHashMap 36map.put("image",R.drawable.gallery_photo_1); 37map.put("title","G1"); 38map.put("info","google1"); 39mHashMaps.add(map); 40 41map=newHashMap 42map.put("image",R.drawable.gallery_photo_2); 43map.put("title","G2"); 44map.put("info","google2"); 45mHashMaps.add(map); 46 47map=newHashMap 48map.put("image",R.drawable.gallery_photo_3); 49map.put("title","G3"); 50map.put("info","google3"); 51 52mHashMaps.add(map); 53returnmHashMaps; 54} 55} simpleAdapter的数据都是用HashMap构成的List,List里面的每一节对应的是ListView的没一行,这里先建一个HashMap构成的List,布局中有3个元素,ImageView,2个TextView,每个item项的布局文件如下: 1 xmlversion="1.0"encoding="utf-8"? > 2 3xmlns: android=" 4android: layout_width="match_parent" 5android: layout_height="match_parent" 6android: orientation="horizontal"> 7 8android: layout_width="wrap_content" 9android: id="@+id/img" 10android: layout_margin="5px" 11android: layout_height="wrap_content"> 12 13 14android: id="@+id/linearLayout1" 15android: layout_width="wrap_content" 16android: layout_height="wrap_content" 17android: orientation="vertical"> 18 19android: id="@+id/title" 20android: layout_width="wrap_content" 21android: layout_height="wrap_content" 22android: textColor="#ffffff" 23android: textSize="22px"> 24 25android: id="@+id/info" 26android: layout_width="wrap_content" 27android: layout_height="wrap_content" 28android: textColor="#ffffff" 29android: textSize="13px"> 30 31 所以有了HashMap构成的数组后,我们要在HashMap中加入数据,按顺序加入图片,title,info,一个HashMap就构成了ListView中的一个Item项,我们在看下API中是怎么描述simpleAdapter的: 第一个参数和第三个参数跟ArrayAdapter中的是一样的,第二个参数就是由HashMap组成的List,也就是数据源,而第5个参数也就是map中的key,最后一个参数就是map中key对应的值要显示在布局中的位置的id。 看下效果: 如果我们想在每个Item中加个button,而且点击button有对应的操作,那该怎么办呢。 这时我们可以重写baseAdapter,看代码: 1packagecom.shang.test; 2 3importjava.util.ArrayList; 4importjava.util.HashMap; 5importjava.util.List; 6 7importandroid.app.Activity; 8importandroid.app.AlertDialog; 9importandroid.content.Context; 10importandroid.content.DialogInterface; 11importandroid.os.Bundle; 12importandroid.view.LayoutInflater; 13importandroid.view.View; 14importandroid.view.View.OnClickListener; 15importandroid.view.ViewGroup; 16importandroid.widget.BaseAdapter; 17importandroid.widget.Button; 18importandroid.widget.ImageView; 19importandroid.widget.ListView; 20importandroid.widget.TextView; 21 22/** 23* 24*@authorshangzhenxiang 25* 26*/ 27publicclassTestBaseAdapterextendsActivity{ 28 29privateListViewmListView; 30 31@Override 32protectedvoidonCreate(BundlesavedInstanceState){ 33super.onCreate(savedInstanceState); 34setContentView(R.layout.baseadapterlist); 35mListView=(ListView)findViewById(R.id.baselist); 36mListView.setAdapter(newBaseListAdapter(this)); 37} 38 39privateList 40List 41HashMap 42map.put("image",R.drawable.gallery_photo_1); 43map.put("title","G1"); 44map.put("info","google1"); 45maps.add(map); 46 47map=newHashMap
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 各种 Adapter 用法