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)

更多相关文章

  1. android 项目R文件丢失解决办法
  2. Android入门教程(十二)之仿百度谷歌搜索自动提示框-----AutoComp
  3. android动态加载Jar/dex--原创
  4. 你需要了解下Android(安卓)View的更新 requestLayout 与重绘 inv
  5. linux环境下android常用工具和技巧
  6. 深入浅出 - Android系统移植与平台开发(五)- 定制手机模拟器ROM
  7. Android(安卓)WebView使用深入浅出
  8. 【Android】AndroidManifest 清单文件以及权限详解
  9. s

随机推荐

  1. Android(安卓)Third Party Libraries and
  2. android操作sdcard
  3. android 自定义view之(一) Creating a View
  4. android service
  5. Android(安卓)弹出对话框Dialog
  6. Android(安卓)源码编译so,jar
  7. 【Android】基础1
  8. Android(安卓)SmartRecyclerView
  9. Android二级分类列表GirdView
  10. Android(安卓)ADT 20.0.0 发布