在Android中,Adapter(适配器)是数据与ui之间的桥梁,它把后台数据与前端ui连接到一起,是一个展示数据的载体。Adapter有很多的接口、抽象类、子类可以使用,以下是常用的几个适配器。
baseAdapter:是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性;
ArrayAdapter:支持泛型操作,最为简单,只能展示一行字。
SimpleAdapter:有最好的扩充性,可以自定义出各种效果。
SimpleCursorAdapter:可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。如需要实现更复杂的UI也可以重写其他方法。可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。
这里,主要介绍SimpleAdapter的简单用法
2.SimpleAdapter
构造方法:
SimpleAdapter(Context context, List extends Map
参数:
1.context:上下文。
2.data:基于Map的list。Data里边的每一项都和 ListView里边的每一项对应。Data里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。
3.resource :就是一个布局layout,可引用系统提供的,也可以自定义。
4.from:这是个名字数组,每个名字是为了在 ArrayList数组的每一个item索引Map
5.to:指定要填充的组件
3.SimpleAdapter实例——创建ListView
(1)main.xml
在布局文件中定义一个ListView
android:layout_width=“match_parent” android:layout_height=“wrap_content” android:orientation=“horizontal”> android:id="@+id/mylist" android:layout_width=“match_parent” android:layout_height=“wrap_content” android:divider="#000" android:dividerHeight=“2dp” android:listSelector="#600" /> (2)simpleadapter.xml android:layout_width=“match_parent” android:layout_height=“wrap_content” android:orientation=“horizontal”> android:id="@+id/text" android:layout_width=“300dp” android:layout_height=“wrap_content” android:padding=“25dp” android:textSize=“20sp” android:textColor="#000" /> android:id="@+id/image" android:layout_width=“80dp” android:layout_height=“80dp” android:padding=“10dp” /> *(3)MainActivity.java package com.example.myapplication;《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取



