[转载自:]http://www.cnblogs.com/transmuse/archive/2010/11/26/1889194.html

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

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

1. ArrayAdapter

public class ArrayAdapter extends BaseAdapter implements Filterable

Class Overview

A ListAdapter that manages a ListView backed by an array of arbitrary(任意的) objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource. However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine (确定)what text will be displayed for the item in the list. To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

一 个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填充了数据

方法:

public static ArrayAdapter<CharSequence> createFromResource (Context context, int textArrayResId, int textViewResId)

Creates a new ArrayAdapter from external resources. The content of the array is obtained through getTextArray(int).Parameters 这个方法能够使用数组xml文件中配置的数据来创建一个ArrayAdapter,这个数组中的内容如何获得,通过this.getResources().getTextArray(id)方法获得。

textArrayResId:The identifier of the array to use as the data source. 自定义数组xml文件的标识id号,也就是ArrayAdapter要绑定到ListVIew中的数据

textViewResId:The identifier of the layout used to create views.// 用于显示数组数据的布局文件的id标识号(注意:该布局文件中只能有一个TextView,有多个就会报错,一般是 ClassCastException)

public View getDropDownView (int position, View convertView, ViewGroup parent)

Get a View that displays in the drop down popup the data at the specified position in the data set.

position:index of the item whose view we want.

convertView:the old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.

parent:the parent that this view will eventually be attached to

Returns
  • a View corresponding (相应的)to the data at the specified position.

更多相关文章

  1. 在Android中查看和管理sqlite数据库
  2. 转载:Android(安卓)service 实现过程
  3. android批量插入数据效率对比
  4. i-jetty环境搭配与编译
  5. Android(安卓)多媒体
  6. mybatisplus的坑 insert标签insert into select无参数问题的解决
  7. 箭头函数的基础使用
  8. python起点网月票榜字体反爬案例
  9. NPM 和webpack 的基础使用

随机推荐

  1. Android基础UI篇------TextView及其子类
  2. android手机屏幕适配方法
  3. android----UI组件
  4. Android基础知识之Manifest文件的组织结
  5. Android教程之Android(安卓)SDK1.5模拟器
  6. Android
  7. Android(安卓)横屏竖屏切换
  8. EditText属性大全
  9. Android:Gravity控制格式
  10. android:maxLines和android:ellipsize同