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. let防止变量声明提前

    let可以解决原来js中,一个函数中变量混乱的问题,因为以前var 定义的变量时, {}是不能限制变量作用域的. "use strict"; +function(){ var t= ...

  2. InnoSetup使用笔记

    今天用InnoSetup做安装包时,因为要装的驱动区分32位.64位,64位系统中要安装32位+64位驱动. 想在脚本中进行判断.折腾一阵,终于搞定: 参考了:http://379910987.blo ...

  3. 关于file.writelines换行符的添加

    和file.readlines/readline不同,file.writelines(l)如果l元素没有换行符,writelines是不会自动加入换行符的,需要我们自己添加,就像这样. import ...

  4. [css小技巧]input去除边框问题

    border:none;是不够的 (1)在谷歌浏览器添加 outline: none;去除点击后产生的边框; (2)IE7下border: none;还会有边框存在,改用border: 0;即可,同时 ...

  5. HDU - 6201:transaction transaction transaction(最长路)

    Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, i ...

  6. python之懒惰属性(延迟初始化)

    Python 对象的延迟初始化是指,当它第一次被创建时才进行初始化,或者保存第一次创建的结果,然后每次调用的时候直接返回该结果.延迟初始化主要用于提高性能,避免浪费计算,并减少程序的内存需求. 1. ...

  7. Codeforces 589F Gourmet and Banquet

    A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...

  8. SSH使用总结(xml配置)

    beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  9. hibernate 一对一(One-to-One)

    一对一(one-to-one)实例(Person-IdCard) 一对一的关系在数据库中表示为主外关系.例如.人和身份证的关系.每个人都对应一个身份证号.我们应该两个表.一个是关于人信息的表(Pers ...

  10. nginx.conf几个示例

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...