1、简介

  Adapter是用来帮助填充数据的中间桥梁,简单点说就是:将各种数据以合适的形式显示到view上,在常见的View(List View,Grid View)等地方都需要用到Adapter!

  初次接触感觉和OC中TableView的cell功能一样!

  继承关系:

BaseAdapter:抽象类,实际开发中我们会继承这个类并且重写相关方法,用得最多的一个Adapter!
ArrayAdapter:支持泛型操作,最简单的一个Adapter,只能展现一行文字~
SimpleAdapter:同样具有良好扩展性的一个Adapter,可以自定义多种效果!
SimpleCursorAdapter:用于显示简单文本类型的listView,一般在数据库那里会用到,不过有点过时, 不推荐使用!

2、简单实例

  对应的Java文件:

public class LoginActivity extends AppCompatActivity  {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
String[] strarr = {"111","111","111","111","111",}; ArrayAdapter<String> arrayAdapter =new ArrayAdapter<String>(this,android.R.layout
.simple_expandable_list_item_1,strarr);
ListView listView = (ListView)findViewById(R.id.listview);
listView.setAdapter(arrayAdapter);
}
}

  ArrayAdapter有五种布局类型:

simple_expandable_list_item_1:  

simple_expandable_list_item_2:  

simple_list_item_checked:     

simple_list_item_multiple_choice: 

simple_list_item_single_choice:  

  每个item的xml布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"> <ImageView
android:id="@+id/imgtou"
android:layout_width="64dp"
android:layout_height="64dp"
android:baselineAlignBottom="true"
android:paddingLeft="8dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:textColor="#1D1D1C"
android:textSize="20sp" /> <TextView
android:id="@+id/says"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8px"
android:textColor="#B4B4B9"
android:textSize="14sp" /> </LinearLayout>
</LinearLayout>

  数据Java文件:

public class LoginActivity extends AppCompatActivity  {

    private String[] names = new String[]{"猪八戒","孙悟空","唐僧"};
private String[] says = new String[]{"饿了","吃俺老孙一棒","紧箍咒"};
private int[] images = new int[]{R.drawable.icon,R.drawable.icon,R.drawable.icon};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
List<Map<String,Object>> listitem = new ArrayList<Map<String,Object>>();
for (int i=0;i<names.length;i++){
Map<String, Object> showitem = new HashMap<String, Object>();
showitem.put("icon",images[i]);
showitem.put("name",names[i]);
showitem.put("say",says[i]);
listitem.add(showitem);
} //public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {}
SimpleAdapter simpleAdapter = new SimpleAdapter(getApplicationContext(),listitem ,R.layout
.list_item,new String[]{"icon","name","say"},new int[]{R.id.imgtou, R.id.name, R
.id.says});
ListView listView = (ListView)findViewById(R.id.listview);
listView.setAdapter(simpleAdapter);
} }

Android数据适配器Adapter简介的更多相关文章

  1. Android数据适配器(Adapter)优化:使用高效的ViewHolder

    原文链接:http://stackvoid.com/using-adapter-in-efficiency-way/ 在使用Listview或GridView的时候,往往须要自己定义数据适配器.一般都 ...

  2. Android 数据适配器

    把复杂的数据(数组.链表.数据库.集合等)填充到指定的视图界面上.   arrayAdapter(数组适配器):      用于绑定一些格式单一的数据,数据源:数据或者集合.   private Li ...

  3. Android学习之Adapter(数据适配器)

    1.定义     数据适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - 下拉列表控件. ...

  4. ListView和Adapter数据适配器的简单介绍

    ListView 显示大量相同格式数据 常用属性: listSelector            listView每项在选中.按下等不同状态时的Drawable divider            ...

  5. Android必学之数据适配器BaseAdapter

    什么是数据适配器? 下图展示了数据源.适配器.ListView等数据展示控件之间的关系.我们知道,数据源是各种各样的,而ListView所展示数据的格式则是有一定的要求的.数据适配器正是建立了数据源与 ...

  6. 浅析android适配器adapter中的那些坑

    做项目中遇到的,折磨了我将近两天,今天把经验分享出来.让大家以后少走点弯路,好了.简单来说一下什么是android的适配器,怎样定义.怎样添加适配器的重用性.怎样去减少程序的耦合性 适配器顾名思义是用 ...

  7. Android 常用数据适配器SimpleAdapter

    在<Android 常用数据适配器ArrayAdapter>中介绍了ArrayAdapter数据适配器.但是存在一个缺陷,那就是条目的图标都固定相同,要显示每个条目的图标都不相同,那么使用 ...

  8. Android 常用数据适配器ArrayAdapter

    接着上篇文章<Android 采用Layout Inflater创建一个View对象>,本文采用常用数据适配器ArrayAdapter 新建项目后,在layout文件夹下新建list_it ...

  9. 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)

    1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...

随机推荐

  1. 14-Ubuntu-文件和目录命令-查看目录内容-ls-1

    1.终端使用技巧 (1)tab键自动补全 (2)按上/下光标键查看使用过的命令 (3)退出当前命令,Ctrl C (4)缩小--Ctrl - ;放大--Ctrl shift + 2.ls 命令--查看 ...

  2. Unity实现Android端视频播放

    本文只讲Android短的视频播放 实现方式 使用Handheld.PlayFullScreenMovie(),这个函数实现.具体如下: 1.创建StreamingAssets文件夹,此文件夹放入视频 ...

  3. NuGet包介绍

    Antlr 各种语言的语法识别器.解析器.编译和翻译器 Microsoft.AspNet.Web.Optimization 绑定优化CSS和JavaScript文件,也就是App_Start下的Bun ...

  4. windows下怎么给ubantu虚拟机全屏的处理

    ubantu版本时16.04 windows下窗口太小需要设置 相信很多人在装虚拟机的时候,遇到了窗口过小不能自适应的问题.我也是查了好多资料,都说安装Vmware Tools即可解决,还有说修改分辨 ...

  5. JS对象 返回/设置时间方法 get/setTime() 返回/设置时间,单位毫秒数 一小时为:60*60*1000

    返回/设置时间方法 get/setTime() 返回/设置时间,单位毫秒数,计算从 1970 年 1 月 1 日零时到日期对象所指的日期的毫秒数. 如果将目前日期对象的时间推迟1小时,代码如下: &l ...

  6. Centos7 pxe

    yum install dnsmasq mv /etc/dnsmasq.conf /etc/dnsmasq.conf.backup # vim /etc/dnsmasq.conf interface= ...

  7. Got permission denied while trying to connect to the Docker daemon

    答案:https://stackoverflow.com/questions/48568172/docker-sock-permission-denied

  8. Java 多线程 - 锁的类型

    https://zhuanlan.zhihu.com/p/37287566 https://www.cnblogs.com/qifengshi/p/6831055.html

  9. csp-s模拟9697题解

    题面:https://www.cnblogs.com/Juve/articles/11790223.html 96: 刚一看以为是水题,直接等差数列求和就好了,然后发现模数不是质数,还要1e18*1e ...

  10. 应用程序正常初始化(0xc0150002)失败的终极解决方案

    转自VC错误:http://www.vcerror.com/?p=62 最近做一个项目写了一个VC6下的MFC程序,结果传到别人的机子上(WIN7)出现了应用程序正常初始化(0xc0150002)失败 ...