版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Android的适配器的学习和使用(整理)在开发中我们需要绑定一些数据展现到桌面上,这是就需要AdapterView 。AdapterView是ViewGroup的子类,它决定了怎么展现视图通过Adapter来绑定特殊的数据类型。AdapterView是非常有帮助的当你展现数据在你的布局中。Gallery,ListView和 Spinner 是 AdapterView 的子类。Suit-rv. NestedInherited XML Attrs 匚onstanlF面看一下 AdapterView的结构图:public abstract classAdapterViewextends Wew右r
2、ouproicLvi 皀b a rtdrd d*vi 亡他 Vi ewG rou pKnown Direct Subclas$Abs List View, AbsSpinner, Adapter ViewAni mato rKnown Indirect Subclass亡各Ad a pterViewF 11 pper, ExpandableUstView, Gallery, GridView, ListView, $pinner(HTTP /WWW XTO.COM然后再看一下Adapter的结构图: public interfaceAdapteran droid.widgetJkdapterK
3、nown Indirect SubclassesArrayAda pter r BaseAdapter, Cu rso rAdapter, Hea derVi ewGstAda pter, ListAdapter, Resou rceCursorAda p SpinnerAdapter, WrapperLi stAda pter上面已经充分展现了他们的子类和父类的基础关系。下面我们看一个 ListViewDemo的例子:先来看一个简单的 adapter的例子: public class SimpleList exte nds ListActivity private String mListS
4、tring=姓名:王魁锋,”性别:男,”年龄:23,居住地:上海市普陀区,”邮箱:;private ListView mListView=n ull;Overrideprotected void on Create(B un dle savedI nsta nceState) / TODO Auto-ge nerated method stubsuper.o nCreate(savedl nsta nceState);mListView=this.getListView();setListAdapter( new ArrayAdapter(this,
5、an droid.R.layout.simpleist_item_1,mListStri ng);mListView.set On ItemClickListe ner(new Onl temClickListe ner() Overridepublic void on ltemClick(AdapterView pare nt. View view,int positi on, long id) / TODO Auto-ge nerated method stub你选择了:Toast.makeText(SimpleList.this,+mListStri ngpositi on, 1).sh
6、ow(););这里用到了系统定义好的适配模式,当然这只能用来简单的数据适配,下面看F效果:56 19:13ListViewDemo姓名:王魁锋 性别:男年龄:23 居住地:上海市普陀区邮箱:wangkuifengOI 18126xitrpublic class IconList extends ListActivity private String mListTitle = 姓名, 性别, 年龄, 居住地 ,邮箱 ;private String mListStr = 王魁锋 , 男, 23, 上海市普陀区;ListView mListView = n
7、ull;ArrayListMap mData= new ArrayListMap();Overrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stub mListView = getListView();int lengh = mListTitle.length;for(int i =0; i lengh; i+) Mapitem = new HashMap();item.put(image, R.drawable.portrait);item.put(title, mLis
8、tTitlei);item.put(text, mListStri); mData.add(item);SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.iconlist, new Stringimage,title,text,new intR.id.image,R.id.title,R.id.text);setListAdapter(adapter); mListView.setOnItemClickListener(new OnItemClickListener()Overridepublicvoid onItemC
9、lick(AdapterView parent, View view,int positi on, long id) / TODO Auto-ge nerated method stubToast.makeText(lco nList.this,您选择了标题:” +mListTitlepositio n + 内容:+mListStrpositio n,Toast.LENGTH_LONG).show(););super.o nCreate(saved In sta nceState);上面的数据可以是同数据库读取的也可以是从网络获取的,这里不做过多介绍看下效果:哈哈 看起来美观了些,如果要做更复
10、杂的布局,哪就要用BaseAdapter 了。先看一下布局文件: 下面是核心代码:public class ColorList extends ListActivity private String mListTitle = 姓名 , 性别 , 年龄 , 居住地 , 邮箱 ;private String mListText= 王魁锋 , 男 ,23, 上海市普陀区 ,;private ListView mListView=null;private MyListAdapter myAdapter=null;Overrideprotected void
11、onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubmListView=this.getListView();myAdapter=new MyListAdapter(this);this.setListAdapter(myAdapter);mListView.setOnItemClickListener(new OnItemClickListener() Overridepublic void onItemClick(AdapterView parent, View view,int position, lo
12、ng id) / TODO Auto-generated method stubView v=parent.getChildAt(position); v.setBackgroundColor(Color.RED);Toast.makeText(ColorList.this, 你选择了 +mListTextposition, 1).show(););super.onCreate(savedInstanceState);private class MyListAdapter extends BaseAdapterprivate Context mContext;private int color
13、s=new int0xff626569,0xff4f5257 ;public MyListAdapter(Context context) mContext=context;Overridepublic int getCount() / TODO Auto-generated method stubreturn mListText.length;Overridepublic Object getItem(int position) / TODO Auto-generated method stubreturn position;Overridepublic long getItemId(int
14、 position) / TODO Auto-generated method stub return position;Overridepublic View getView(int position, View convertView, ViewGroup parent) ImageView image=null;TextView title=null;TextView content=null;if(convertView=null)convertView=LayoutInflater.from(mContext).inflate(R.layo ut.colorlist, null);i
15、mage=(ImageView) convertView.findViewById(R.id.color_image);title=(TextView) convertView.findViewById(R.id.color_title);content=(TextView) convertView.findViewById(R.id.color_text);int colorPos=position%colors.length; convertView.setBackgroundColor(colorscolorPos); title.setText(mListTitleposition);
16、content.setText(mListTextposition); image.setImageResource(R.drawable.portrait);return convertView;BaseAdapter 可以让我们做比较复杂的布局, 只要在 xml 文件中设置好布局格式,在 getView 中分别取出放入相应的值就可以了。下面看一些效果:你选择了王魁IS礙關H?/- TTTWWWWJgrsJ还有一些 SpinnerAdapter 和SimpleCursorAdapter 等系统自带的适配器,都是比较简单的,可以看下 API自行练习一下,这里特别说明一下,从数据库里取出的数据最
17、好直接放入 SimpleCursorAdapter 很方便的摘自wangkuifeng0118 的专栏常用适配器总结(摘自开发者社区)一,适配器顾名思义,就是把一些数据给弄得适当,适合以便于在View上显示。可以看作是界面数据绑定的一种理解。它所操纵的数据一般都是一些比较复杂的数据,如数组,链表,数据库,集合等。适配器就像显示器,把复杂的东西按人可以接受的方式来展现。那么适配器是怎么处理得到的数据, 并把它显示出来的呢。 其实很简单,说白了适配器它也是一个类, 在类里面它实现了父类的这几个方法:publicint getCount() /得到数据的行数public Object getItem
18、(int position) 根据 position 得到某一行的记录public long getltemld(int position) 得到某一条记录的ID/下面这个方法是最重要的相比于其它几个方法,它显式的定义了,适配器将要以什么样的 方式去显示我们所填充的数据 ,在自定义的适配器里面我们通常会给它写个布局文件 publicView getView(int position, View convertView, V iewGroup parent)我们常用的适配器一共有三个, 当然不包含自定义的适配器, 哪三个 那就是 ArrayAdapter , SimpleAdapter , Si
19、mpleCursorAdapter 这三个,他们都是继承 BaseAdapter 其中以 ArrayAdapter 最为简单,只能展示一行字。 SimpleAdapter 有最好的扩充性,可以自 定义出各种效果。 SimpleCursorAdapter 可以认为是 SimpleAdapter 对数据库的简单结合,可 以方面的把数据库的内容以列表的形式展示出来。 二,一般对于前两个适配器,他们的数据来源无非就是 String 或者 List 。下面我们列举两 个例一子: 例一,数组作为数据源,填充的是 ArrayAdapterpublic class Example extendsListAct
20、ivityString sex = new String() 男 , “女 ” /数/ 据源ArrayAdapter adapter;/ 数组适配器,用的是泛型public voidonCreate(Bundle SavedInstanceState)super.onCreate(SavedInstanceStat);/ 在 对 适 配 器 初 始 化 的 时 候 , 顺 便 把 数 据 源 装 载 到 适 配 器 里, /this.android.R.Layout.Simple_List_Item_1 这句话 /的意思是将数据源以系统定义好的样式放到适配器里.adapter=newArray
21、AdapterString(this.android.R.Layout.Simple_List_Item_1,sex); this.setAdapter(adapter);/ 这是一个控件类, 所以可以直接将适配器绑定到本身 对象中。例二: List 作为数据源,填充的是 SimpleAdapterListView list = (ListV iew)findView ById(R.id.MyListView);/ 生成动态数组,并且转载数据ArrayListHashMap mylist = newArrayListHashMap();for(int i=0;i30;i+) HashMapma
22、p = new HashMap(); map.put(ItemTitle,This is Title);map.put(ItemText,This is text);mylist.add(map);/生成适配器,数组 = ListItemSimpleAdapter mSchedule = new SimpleAdapter(this, / 没什么解释 mylist,/ 数据来源 R.layout.my_listitem,/ListItem 的 XML 实现 /动态数组与 ListItem 对应的子 项new StringItemTitle, ItemText, /ListItem 的 XML
23、文件里面的两个TextView ID new int R.id.ItemTitle,R.id.ItemText);/ 添加并且显示list.setAdapter(mSchedule);三,应该说着两个例子都不难, 都是一些我们经常见到的用法, 那么对于 SimpleCursorAdapter 又是怎么用的呢, SimpleCursorAdapter 一般主要用于数据库, 它的数据来源一般都是数据库 查询得到的 Cursor 我们来看下面的例子:String uriString = “ content:/contacts/people/ ”;Cursor myCursor =managedQue
24、ry(Uri.parse(uriString), null, null, null, null);String fromColumns = new StringPeople.NUMBER, People.NAME;int toLayoutIDs = new int R.TextView, R.id.numberTextV iew; SimpleCursorAdapter myAdapter; myAdapter=newSimpleCursorAdapter(this,R.layout.simplecursorlayout,myCurso r,fromColumns,toLayou
25、tIDs);/ 传入当前的上下文、一个 layout 资源,一个游标和两个数组: 一个包含使用的列/的名字,另一个(相同大小)数组包含 View 中的资源 ID ,用于显示相应列 的数据值。myListView.setAdapter(myAdapter);我们把一个游标绑定到了 ListView 上,并使用自定义的 layout 显示来显示每一个 Item 。四,下面我们来定义自己的适配器。 为什么要定义自己的适配器呢,原因就在于,当我们想用一些其它的展现方式, 或者是我们需要的,呈现方式,这是就得 DIY 了。首先我们定义一个类让它继承自BaseAdapter,再让它实现一里面所说的那几个方
26、法。那么这个自定义适配器就算好了。里面的一些方法我在上面都介绍过了,在这就不在赘述了。public class ImageAdapter extendsBaseAdapter private Contextmcontext;/构造函数里面有两个参数,一个是数据的来源,另一个是上 下文。public ImageAdapter(Integer imgIds,Context c) mcontext=c; imageIds=imgIds;publicint getCount() / TODO Auto-generated method stubreturn imageIds.length;public
27、Object getItem(int position) / TODO Auto-generated method stub return null;publiclong getItemId(int position) / TODO Auto-generated method stubreturn position;/ 主要工作是做在这里,可以自定义布局,在这里我就不多说了publicView getView(int position, View convertV iew, ViewGroup parent) / TODO Auto-generated method stub ImageVie
28、w imageview = newImageView(mcontext); imageview.setImageResource(imageIdsposition); imageview.setLayoutParams(newGallery.LayoutParams(120,120); imageview.setScaleType(ImageView.ScaleType.FIT_CENTER); return imageview;转载自:/transmuse/archive/2010/11/26/1889194.htmlAndroid 中有很多的适配器
29、,首先看看这些适配器的继承结构ArrayAdapterrurjsorAdriptprSiqpleAdapler in i ciI -LiAtAdAptrSpinne* irAdnptfkrinierface*rapperliftAdaptersplay, for instanee, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of vi ew you want.一个l
30、istAdapter用来管理一个用一组任意对象的数组填充的ListView。默认的ListAdapter希望提供的ListView每一项的xml布局配置文件中只有一个TextView ,如果你想使用一个符合布局的话,你就要使用含有id字段的构造函数了,这个id要去引用这个复杂布局文件中的一个TextView ,TextView 被引用了,使用数组中的对象,调用toString方法,转换成字符串来填充这个TextView,你可以使用包含自定义对象的数组或者集合。重写自定义对象的toString()方法,来保证ListView显示。你也可以是使用其他的一些非TextView控件来显示数组中的数据,
31、例如ImageViews,通过重写 Adapter的getView方法来得到你想要的view。构造函数:public ArrayAdapter (Con text con text, int textViewResourceld)con text : The curre nt con text.当期的上下文对象textViewResourceId : The resource ID for a layout file containing a TextView to use w hen instantiatingviews. 一个包含了 TextView 的布局xml文件的id,注意(这个布局
32、文件里只能有TextView 一个控件,TextView 不能有父控件,否则会报错java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView )类似于这种的xmlpublic ArrayAdapter (Con text con text, int textViewResourceId, T object s)objects :用来填充 ListView,给ArrayAdapter提供数据的数组public ArrayAdapter (Con text con text, int
33、 textViewResourceId, List o bjects) /建议使用这个,直接给ArrayAdapter填充了数据/*public ArrayAdapter (Con text con text, int resource, int textViewResourceI d)resource : The resource ID for a lay out file containing a layout to use when instantiati ng views. ListView 中Item 项的复杂布局 xml文件textViewResourceld : The id o
34、f the TextView within the layout resource to be populated (显示)ListView 中Item 项的复杂布局 xml文件中用来显示 ArrayAdapter 中数据的那个 TextV iewpublic ArrayAdapter d, T objects)(Con text con text,intresource,int textViewResourceIpublic ArrayAdapter d, List objects)/(Con text con text, int建议使用这个,直接给resource, int textVie
35、wResourceI ArrayAdapter 填充了数据方法:public static ArrayAdapter vCharSequence createFromResource (Context con text, int textArrayResId, int textViewResId)Creates a new ArrayAdapter from external resources. The content of the array is obtai ned through getTextArray(int) .Parameters 这个方法能够使用数组 xml文件中配置的数 据
36、来创建一个 ArrayAdapter ,这个数组中的内容如何获得,通过this.getResources().getTextArray (id)方法获得。textArray ResId : The identifier of the array to use as the data source. 自定义数组 xml文件的标识id号,也就是ArrayAdapter 要绑定到ListVIew 中的数据textViewResId : The identifier of the layout used to create views./用于显示数组数据的布局文件的id标识号(注意:该布局文件中只能有一个TextView,有多个就会报错,一般是 ClassCastException)public View getDropDownView (int position, View convertView, ViewGro up pare nt)positi oninGet a View that display
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026春季江铜集团法务风控部校园招聘2人备考题库(第二批)及答案详解(网校专用)
- 2026江西赣州寻乌县中共城市社区工作委员会招聘28人备考题库及答案详解(夺冠系列)
- 四川省阿坝州汶川县公开招聘乡镇残联专干备考题库(2人)附答案详解(突破训练)
- 2026新华人寿保险股份有限公司宜宾中心支公司续期服务人员招聘6人备考题库含答案详解(突破训练)
- 2026广东外语外贸大学招聘事业编制工作人员31人备考题库带答案详解
- 2026福建福州新区(长乐区)卫健教育系统招聘医学类专业人员60人备考题库附答案详解(基础题)
- 2026云南安宁化工厂有限公司校园招聘5人备考题库含答案详解(培优)
- 2026浙江宁波市璟诚企业运营管理有限公司劳务派遣招聘1人备考题库及答案详解(全优)
- 2024年北京市东城区小升初数学试卷
- 20中国农业大学植物抗逆高效全国重点实验室大豆研究中心博士后招聘备考题库含答案详解(模拟题)
- 2024年甘肃省白银市、武威市、嘉峪关市、临夏州中考物理试题及答案
- 公安安全教育开学第一课
- 医药公司自提药品管理制度
- 容量评估与液体管理
- 抖音电商200个干货问题知识手册内部资料
- 刑法学知到智慧树章节测试课后答案2024年秋江西师范大学
- 2024年南昌二手房购买协议一
- 瓦斯隧道安全培训
- 2024年铁路机车司机乘务员知识(机考)试题库(含答案)
- 幼儿园 中班语言绘本《章鱼先生卖雨伞》
- 零星维修工程项目施工方案1
评论
0/150
提交评论