[转]Android 适配器Adapter的学习

来源:互联网 发布:兰德马克软件招聘 编辑:程序博客网 时间:2024/04/24 06:41

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)

原创粉丝点击