Android中有很多的适配器,首先看看这些适配器的继承结构

  这些适配器中,BaseAdapter用的最多,也用的最熟,先放过他,从ArrayAdapter开始

  一个listAdapter用来管理一个用一组任意对象的数组填充的ListView。默认的ListAdapter希望提供的 ListView每一项的 xml布局配置文件中只有一个TextView,如果你想使用一个符合布局的话,你就要使用含有id字段的构造函数了,这个id要去引用这个复杂布局文件 中的一个TextView,TextView被引用了,使用数组中的对象,调用toString方法,转换成字符串来填充这个TextView,你可以使 用包含自定义对象的数组或者集合。重写自定义对象的toString()方法,来保证ListView显示。你也可以是使用其他的一些非TextView 控件来显示数组中的数据,例如ImageViews,通过重写Adapter的getView方法来得到你想要的view。

  构造函数:

  public ArrayAdapter (Context context, int textViewResourceId)

  context:  The current context. 当期的上下文对象

  textViewResourceId:  The resource ID for a layout file containing a TextView to use when instantiating views. 一个包含了TextView的布局xml文件的id,注意(这个布局文件里只能有TextView一个控件,TextView不能有父控件,否则会报错 java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView)

  类似于这种的xml

  <?xml version="1.0" encoding="utf-8"?>
  <TextView android:id="@+id/subject"
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:layout_marginTop="5dip" android:textAppearance="?android:attr/textAppearanceMedium"
      android:singleLine="true" android:ellipsize="end" />

  public ArrayAdapter (Context context, int textViewResourceId, T[] objects)

  objects:用来填充ListView,给ArrayAdapter提供数据的数组

  public ArrayAdapter (Context context, int textViewResourceId, List<T> objects) //建议使用这个,直接给ArrayAdapter填充了数据

  public ArrayAdapter (Context context, int resource, int textViewResourceId)

  这个是用来复杂布局的,ListView的Item项的布局文件中不止含有一个TextView控件

  resource: The resource ID for a layout file containing a layout to use when instantiating views. ListView中Item项的复杂布局xml文件

  textViewResourceId:The id of the TextView within the layout
resource to be populated(显示)
ListView中Item项的复杂布局xml文件中用来显示ArrayAdapter中数据的那个TextView

  public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

  public ArrayAdapter (Context context, int resource, int
textViewResourceId, List<T> objects)//建议使用这个,直接给ArrayAdapter填充了数据。

  方法:

  这个方法能够使用数组xml文件中配置的数据来创建一个ArrayAdapter,这个数组中的内容如何获得,通过this.getResources().getTextArray(id)方法获得。

  自定义数组xml文件的标识id号,也就是ArrayAdapter要绑定到ListVIew中的数据

  用于显示数组数据的布局文件的id标识号(注意:该布局文件中只能有一个TextView,有多个就会报错,一般是 ClassCastException)

 

Android适配器Adapter的学习的更多相关文章

  1. Android适配器Adapter学习

    在开发中我们需要绑定一些数据展现到桌面上,这是就需要AdapterView.AdapterView是ViewGroup的子类,它决定了怎么展现视图通过Adapter来绑定特殊的数据类型. Adapte ...

  2. android 适配器 BaseAdapter 的学习

    昨天晚上看了下ArrayAdapter,和SimpleAdapter,今天早上起来看完了球赛,又继续要开始学习了,适配器除了前面的两种,还有一种常用的就是BaseAdapter,他是一个抽象类.事实上 ...

  3. android适配器Adapter

    一.什么是适配器,适配器有什么用? 适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - ...

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

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

  5. android 适配器 ArrayAdapter,SimpleAdapter的学习

    今天认真看了下android适配器,学习了下它的使用方法. 一,ArrayAdapter ArrayAdapter 比较简单,只可以存放一行文本信息.下面是简单的实现 private ListView ...

  6. android中Adapter适配器的讲解

    Adapter(适配器的讲解) 适配器就我自己来看,我觉得这是一个非常重要的知识点,Adapter是用来帮助填出数据的中间桥梁,简单点说吧:将各种数据以合适的形式显示在View中给用户看.Adapte ...

  7. 设计模式学习心得<适配器 Adapter>

    适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁.这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能. 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接 ...

  8. Android Listview & Adapter

    Listview主要有两个职责: 将数据填充到布局 处理用户的选择点击等操作 列表的显示需要三个元素: ListVeiw 用来展示列表的View 适配器(Adapter) 用来把数据映射到ListVi ...

  9. Android之Adapter用法总结-(转)

    Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...

随机推荐

  1. iostat使用心得

    iostat用于输出CPU和磁盘I/O相关的统计信息. 命令格式: iostat [ -c | -d ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ device [ ... ...

  2. H264的start code是什么?

    H.264起始码 在网络传输h264数据时,一个UDP包就是一个NALU,解码器可以很方便的检测出NAL分界和解码.但是如果编码数据存储为一个文件,原来的解码器将无法从数据流中分别出每个NAL的起始位 ...

  3. ASP调用存储过程访问SQL Server

     ASP调用存储过程访问SQL Server 2011-02-15 10:22:57 标签:asp 数据库 sQL 存储过程 Server ASP和存储过程(Stored Procedures)的文章 ...

  4. 【解题报告】[动态规划]RQNOJ - PID82 / 又上锁妖塔

    原题地址:http://www.rqnoj.cn/problem/82 解题思路: 简单的动态规划 状态表示:DP[i][0]表示当前在第i层,且当前跳跃状态不可用,此时消耗的最短时间.   DP[i ...

  5. JSONP学习笔记

    1. 实现跨域请求的三种方法 本地代理.Flash.Script标签(img,iframe,link也可以,具有src属性) 2. JSONP全称:JSON With Padding(使用内嵌的方式把 ...

  6. Markdown 中的目录自动生成功能 TOC

    目录 Markdown 中的目录自动生成功能 TOC 1. 标题一 1.1 标题二 1.标题二 2. 标题一 2.1 标题二 2.2 标题二 Markdown 中的目录自动生成功能 TOC 1. 标题 ...

  7. The method of type must override a superclass method解决方式(转)

    今天做struts2上传文件的时候出现了这个问题: The method execute() of type UploadAction must override or implement a sup ...

  8. Voting and Shuffling to Optimize Atomic Operations

    2iSome years ago I started work on my first CUDA implementation of the Multiparticle Collision Dynam ...

  9. 【openCV学习笔记】在Mac上配置openCV步骤详解

    (1)安装Homebrew:(需要Ruby) 注:因为snow leopard 以后已经自带Ruby了,所有可以不用自己安装Ruby. 看一下Homebrew的官网: http://mxcl.gith ...

  10. EasyWeChat微信开放平台第三方平台接入

    EasyWeChat微信开放平台第三方平台接入 https://www.cnblogs.com/bainiu/p/8022729.html