2012-08-10

Android Adapter家族系列(一) - Adapter(適配器)

Adapter(適配器)  官方網站上有對它的一些類別概述:
適配器對象作為AdapterView之間的一個橋樑,並為這一觀點的基礎數據。適配器提供的數據項的訪問。適配器也作出負責的數據集給每個項目。

簡單的來說就是資料和元件間溝通的橋樑,Adapter有以下幾種間接子類別:
ArrayAdapterBaseAdapterCursorAdapterHeaderViewListAdapterListAdapterResourceCursorAdapterSimpleAdapter,SimpleCursorAdapterSpinnerAdapterWrapperListAdapter
這些子類別我們在後續章節再做各別的介紹。

我們常會在ListView、Spinner...等看到這一類別,而且也必須使用到它
我們就來看它常用的公用方法:


getCount ()

How many items are in the data set represented by this Adapter.
適配器內的資料個數

程式碼
public  int getCount() {
   return title.length;
}


GetItem (位置)


Get the data item associated with the specified position in the data set.
獲取的數據在指定位置的數據集相關的項目。


程式碼
public Object getItem(int position) {
  return title[position];
}


getItemId (int position)



Get the row id associated with the specified position in the list.

獲取的行ID列表中的指定位置。

Parameters
position  The position of the item within the adapter's data set whose row id we want.
放置在適配器的數據集的行ID,我們希望該項目的位置。

Returns
The id of the item at the specified position.

返回指定位置編號的項目。

程式碼
public long getItemId(int position) {
   return position;
}


getItemViewType (int position)


Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.
獲取由getView(INT,查看,ViewGroup的)創建的視圖類型的指定項目


參數
position  該項目內適配器的數據集,我們想要的視圖類型的位置


程式碼
public int getItemViewType(int position) {
  return position ;
}


getView int position, View convertView, ViewGroup parent



Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use inflate(int, android.view.ViewGroup, boolean) to specify a root view and to prevent attachment to the root.
得到一個視圖顯示在指定位置的數據集的數據。你可以手動創建一個視圖從一個XML佈局文件
Parameters (參數)
position(位置)The position of the item within the adapter's data set 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. Heterogeneous lists can specify their number of view types, so that this View is always of the right type (see getViewTypeCount() and getItemViewType(int)).
簡單來說就是一個view的容器,可以引用新的XML佈局資料達到自定義佈局,再載入資料,最後回傳給需要的元件使用。

parent The parent that this view will eventually be attached to
Returns(返回)
  • A View corresponding to the data at the specified position.  
  • 查看相應的數據在指定的位置
程式碼
public View getView(int position, View convertView, ViewGroup parent) {

  //自訂類別,表達個別listItem中的view物件集合。
        ViewTag viewTag;

        if(convertView == null){
            //取得listItem容器 view
            convertView = myInflater.inflate(R.layout.adapter, null);
            
            //建構listItem內容view
            viewTag = new ViewTag(
            (ImageView)convertView.findViewById(R.id.MyAdapter_ImageView_icon),
            (TextView) convertView.findViewById(R.id.MyAdapter_TextView_title),
            (TextView) convertView.findViewById(R.id.MyAdapter_TextView_info)
             );
            
            //設置容器內容
            convertView.setTag(viewTag);
        }
        else{
            viewTag = (ViewTag) convertView.getTag();
        }
        
        //設定內容圖案
        switch(position){
            case 0:
                viewTag.icon.setBackgroundResource(R.drawable.taipei);
                break;
            case 1:
                viewTag.icon.setBackgroundResource(R.drawable.taichung);
                break;
            case 2:
                viewTag.icon.setBackgroundResource(R.drawable.kaohsiung);
                break;
        }
        
        //設定標題文字
        viewTag.title.setText(title[position]);
        //設定內容文字
        viewTag.info.setText(info[position]);
        
        return convertView;
 }

後記:

Adapter(適配器)還蠻常會看到的,因為如果想客製化介面元件的話,一般都需要用到Adapter(適配器)改變元件為自定義形式,最常用的就是BaseAdapter這個子類,一般形態ArrayAdapter也蠻常用的,不過ArrayAdapter都是用在沒什麼改變時調用,如果想自定義一般都會做用到BaseAdapter這個子類,後續我們再逐步介紹這兩個子類別,謝謝。



1 則留言:

  1. Jim 您好,謝謝您這篇教學。
    有一個看不懂的地方想請問您:
    public Object getItem(int position) {
    return title[position];
    請問這段的Object是指回傳的是一個物件嗎?我對這裡看得霧沙沙的。因為我的理解是:回傳陣列中某位置的值,但對這樣的宣告仍看不懂public Object getItem(int position)。
    希望您能教我一下?

    回覆刪除

您的寶貴建議是我前進的動力!