1、简介

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

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

  继承关系:

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

2、简单实例

  对应的Java文件:

  1. public class LoginActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_login);
  6. String[] strarr = {"111","111","111","111","111",};
  7.  
  8. ArrayAdapter<String> arrayAdapter =new ArrayAdapter<String>(this,android.R.layout
  9. .simple_expandable_list_item_1,strarr);
  10. ListView listView = (ListView)findViewById(R.id.listview);
  11. listView.setAdapter(arrayAdapter);
  12. }
  13. }

  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布局文件:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:orientation="horizontal">
  7.  
  8. <ImageView
  9. android:id="@+id/imgtou"
  10. android:layout_width="64dp"
  11. android:layout_height="64dp"
  12. android:baselineAlignBottom="true"
  13. android:paddingLeft="8dp" />
  14.  
  15. <LinearLayout
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:orientation="vertical">
  19.  
  20. <TextView
  21. android:id="@+id/name"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:paddingLeft="8dp"
  25. android:textColor="#1D1D1C"
  26. android:textSize="20sp" />
  27.  
  28. <TextView
  29. android:id="@+id/says"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:paddingLeft="8px"
  33. android:textColor="#B4B4B9"
  34. android:textSize="14sp" />
  35.  
  36. </LinearLayout>
  37. </LinearLayout>

  数据Java文件:

  1. public class LoginActivity extends AppCompatActivity {
  2.  
  3. private String[] names = new String[]{"猪八戒","孙悟空","唐僧"};
  4. private String[] says = new String[]{"饿了","吃俺老孙一棒","紧箍咒"};
  5. private int[] images = new int[]{R.drawable.icon,R.drawable.icon,R.drawable.icon};
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_login);
  10. List<Map<String,Object>> listitem = new ArrayList<Map<String,Object>>();
  11. for (int i=0;i<names.length;i++){
  12. Map<String, Object> showitem = new HashMap<String, Object>();
  13. showitem.put("icon",images[i]);
  14. showitem.put("name",names[i]);
  15. showitem.put("say",says[i]);
  16. listitem.add(showitem);
  17. }
  18.  
  19. //public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {}
  20. SimpleAdapter simpleAdapter = new SimpleAdapter(getApplicationContext(),listitem ,R.layout
  21. .list_item,new String[]{"icon","name","say"},new int[]{R.id.imgtou, R.id.name, R
  22. .id.says});
  23. ListView listView = (ListView)findViewById(R.id.listview);
  24. listView.setAdapter(simpleAdapter);
  25. }
  26.  
  27. }

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. 04E: Sub-process /usr/bin/dpkg returned an error code (1)

  2. java获得磁盘、网络实时I/O速率

    最近项目中需要一个平台硬件资源的监控模块,当时采用了Sigar中api,但是做到针对磁盘和网络的实时I/O速率的时候发现Sigar并没有直接支持的接口.于是……它就诞生了.底层采用C++编写,通过ja ...

  3. VBA当中的时间日期函数

    目前还没发现VBA中有直接的函数能够将完整的年月日时分秒的文本格式日期转换成日期型日期的,那只能使用间接实现的办法.用dateserial + timeserial的方法.因为dateserial和t ...

  4. anaconda新建环境

    安装tensorflow等如下: https://blog.csdn.net/Gransand/article/details/80713810 修改默认打开目录如下: https://blog.cs ...

  5. delphi 第4课

    try 语句;(正常)except 语句; (意外处理部分) end: 例子: begin sum:=; try n:=strtoint(edit1.Text); except showMessage ...

  6. QueryList 来做采集

    示例代码 先来感受一下使用 QueryList 来做采集是什么样子. 1 采集百度搜索结果列表的标题和链接.大理石平台价格 采集代码: $data = QueryList::get('https:// ...

  7. thinkphp 模板继承

    模板继承是一项更加灵活的模板布局方式,模板继承不同于模板布局,甚至来说,应该在模板布局的上层.模板继承其实并不难理解,就好比类的继承一样,模板也可以定义一个基础模板(或者是布局),并且其中定义相关的区 ...

  8. create table常用命令

    CREATE TABLE students( stuID INTEGER NOT NULL , stuname ) not null, sex int NOT NULL ); CREATE TABLE ...

  9. H5 小代码(实时更新)

    :before { content: ''; display: inline-block; vertical-align: middle; height: %; } ↑  自适应垂直居中 .clear ...

  10. vue-grid-layout

    vue-grid-layout    vue-grid-layout is a grid layout system, like Gridster, for Vue.js. Heavily inspi ...