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

image

这些适配器中,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 文件操作,删除,拷贝文件等
  2. Android系统源码编译全过程——下载Android源文件并编译
  3. Android jni中数组参数的传递方式
  4. [Android] 文件夹下文件的个数限制
  5. Android之Telephony各文件解释
  6. Android 自定义View及其在布局文件中的使用示例(二)

随机推荐

  1. 【Android语音合成TTS】百度语音接入方法
  2. Android(安卓)基础教程之---动态更改屏幕
  3. QT5.x应用在Mac OS X和Windows平台的发布
  4. Android 软件开发之如何使用Eclipse Debu
  5. Android 视频 美颜SDK对比
  6. Android 动态解析网络布局
  7. Android Studio中新建assets文件的两种方
  8. Android OpenGL ES 基础:绘制三角形
  9. Android中高效的显示图片之二——在非UI
  10. 通过wifi连接android设备的方法