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