ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:193.83KB ,
资源ID:8644244      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8644244.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(androidadapter学习笔记.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

androidadapter学习笔记.docx

1、androidadapter学习笔记Android之Adapter用法总结1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带。在常见的View(ListView,GridView)等地方都需要用到Adapter。如下图直观的表达了Data、Adapter、View三者的关系:Android中所有的Adapter一览: 由图可以看到在Android中与Adapter有关的所有接口、类的完整层级图。在我们使用过程中可以根据自己的需求实现接口或者继承类进行一定的扩展。比较常用的有 BaseAdapter,SimpleAdapter,ArrayAd

2、apter,SimpleCursorAdapter等。 BaseAdapter是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性; ArrayAdapter支持泛型操作,最为简单,只能展示一行字。 SimpleAdapter有最好的扩充性,可以自定义出各种效果。 SimpleCursorAdapter可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。如需要实现更复杂的UI也可以重写其他方法。可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。 2.应用案例1)ArrayAdapter列表的显示

3、需要三个元素:aListVeiw 用来展示列表的View。b适配器用来把数据映射到ListView上的中介。c数据具体的将被映射的字符串,图片,或者基本组件。案例一public class ArrayAdapterActivity extends ListActivity Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); /列表项的数据 String strs = 1,2,3,4,5; ArrayAdapter adapter = new ArrayAdapt

4、er(this,android.R.layout.simple_expandable_list_item_1,strs); setListAdapter(adapter); 案例二 public class MyListView extends Activity private ListView listView; /private List data = new ArrayList(); Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); listView =

5、 new ListView(this); listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1,getData(); setContentView(listView); private List getData() List data = new ArrayList(); data.add(测试数据1); data.add(测试数据2); data.add(测试数据3); data.add(测试数据4); return data; 上面代码使用了ArrayAdapter

6、(Contextcontext, int textViewResourceId,List objects)来装配数据,要装配这些数据就需要一个连接ListView视图对象和数组数据的适配器来两者的适配工作,ArrayAdapter的构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。同时用setAdapter()完成适配的最后工作。效果图如下:2)SimpleAdaptersimpleAdapter的扩展性最好,可以定义各

7、种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。案例一simple.xmlpublic class SimpleAdapterActivity extends ListActivity Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceSt

8、ate); SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.simple, new String title, img , new int R.id.title, R.id.img ); setListAdapter(adapter); private ListMap getData() /map.put(参数名字,参数值) ListMap list = new ArrayListMap(); Map map = new HashMap(); map.put(title, 摩托罗拉); map.put(im

9、g, R.drawable.icon); list.add(map); map = new HashMap(); map.put(title, 诺基亚); map.put(img, R.drawable.icon); list.add(map); map = new HashMap(); map.put(title, 三星); map.put(img, R.drawable.icon); list.add(map); return list; 案例二下面的程序是实现一个带有图片的类表。首先需要定义好一个用来显示每一个列内容的xml,vlist.xml public class MyListVi

10、ew3 extends ListActivity / private List data = new ArrayList(); Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist, new Stringtitle,info,img, new intR.id.title,R.id.info,R.id.img); setLi

11、stAdapter(adapter); private ListMap getData() ListMap list = new ArrayListMap(); Map map = new HashMap(); map.put(title, G1); map.put(info, google 1); map.put(img, R.drawable.i1); list.add(map); map = new HashMap(); map.put(title, G2); map.put(info, google 2); map.put(img, R.drawable.i2); list.add(m

12、ap); map = new HashMap(); map.put(title, G3); map.put(info, google 3); map.put(img, R.drawable.i3); list.add(map); return list; 使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局vlist.xml。下面做适配,new一个SimpleAdapter参数一次是:this,布局文件(vl

13、ist.xml),HashMap的 title 和 info,img。布局文件的组件id,title,info,img。布局文件的各组件分别映射到HashMap的各元素上,完成适配。运行效果如下图:3)SimpleCursorAdapterpublic class SimpleCursorAdapterActivity extends ListActivity Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); /获得一个指向系统通讯录数据库的Cursor对象获

14、得数据来源 Cursor cur = getContentResolver().query(People.CONTENT_URI, null, null, null, null); startManagingCursor(cur); /实例化列表适配器 ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cur, new String People.NAME, new int android.R.id.text1); setListAdapter(adapter); 一

15、定要以数据库作为数据源的时候,才能使用SimpleCursorAdapter,这里特别需要注意的一点是:不要忘了在AndroidManifest.xml文件中加入权限效果如下:4)BaseAdapter有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮。添加按钮首先要写一个有按钮的xml文件,然后自然会想到用上面的方法定义一个适配器,然后将数据映射到布局文件上。但是事实并非这样,因为按钮是无法映射的,即使你成功的用布局文件显示出了按钮也无法添加按钮的响应,这时就要研究一下ListView是如何现实的了,而且必须要重写一个类继承BaseAdapter。下面的示例将显示一个按钮和一个图片

16、,两行字如果单击按钮将删除此按钮的所在行。并告诉你ListView究竟是如何工作的。vlist2.xml /*002 * author 003 *004 */005 public class MyListView4 extends ListActivity 006 007 008 private ListMap mData;009 010 Override011 public void onCreate(Bundle savedInstanceState) 012 super.onCreate(savedInstanceState);013 mData = getData();014 MyAd

17、apter adapter = new MyAdapter(this);015 setListAdapter(adapter);016 017 018 private ListMap getData() 019 ListMap list = new ArrayListMap();020 021 Map map = new HashMap();022 map.put(title, G1);023 map.put(info, google 1);024 map.put(img, R.drawable.i1);025 list.add(map);026 027 map = new HashMap()

18、;028 map.put(title, G2);029 map.put(info, google 2);030 map.put(img, R.drawable.i2);031 list.add(map);032 033 map = new HashMap();034 map.put(title, G3);035 map.put(info, google 3);036 map.put(img, R.drawable.i3);037 list.add(map);038 039 return list;040 041 042 / ListView 中某项被选中后的逻辑043 Override044

19、protected void onListItemClick(ListView l, View v, int position, long id) 045 046 Log.v(MyListView4-click, (String)mData.get(position).get(title);047 048 049 /*050 * listview中点击按键弹出对话框051 */052 public void showInfo()053 new AlertDialog.Builder(this)054 .setTitle(我的listview)055 .setMessage(介绍.)056 .s

20、etPositiveButton(确定, new DialogInterface.OnClickListener() 057 Override058 public void onClick(DialogInterface dialog, int which) 059 060 )061 .show();062 063 064 065 066 067 public final class ViewHolder068 public ImageView img;069 public TextView title;070 public TextView info;071 public Button viewBtn;072 073 074 075 public class MyAdapter extends BaseAdapter076 077 private LayoutInflater mInflater;078 079 080 public MyAdapter(Context context)081 this.mInflater = LayoutInflater.from(context);082 083 Override

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1