Android 適配器Adapter的學習

來源:互聯網
上載者:User

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.
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.