1. 程式人生 > >android介面卡Adapter

android介面卡Adapter

一.什麼是介面卡,介面卡有什麼用?

介面卡是AdapterView檢視(如ListView - 列表檢視控制元件、Gallery - 縮圖瀏覽器控制元件、GridView - 網格控制元件、Spinner - 下拉列表控制元件、AutoCompleteTextView - 自動提示文字框、ExpandableListView - 支援展開/收縮功能的列表控制元件等)與資料之間的橋樑,用來處理資料並將資料繫結到AdapterView上。
android提供多種介面卡,開發時可以針對資料來源的不同採用最方便的介面卡,也可以自定義介面卡完成複雜功能。

補充:

AdapterView物件有兩個主要任務
    1. 在佈局中顯示資料
    2. 處理使用者的選擇


BaseAdapter一般的介面卡基類可用於將資料繫結到listview、Gallery、GridView 、spinner、AutoCompleteTextView上,當然也可以繫結到ExpandableListView上
BaseExpandableListAdapter可擴充套件的介面卡基類可用於將資料繫結到支援展開/收縮功能的列表控制元件ExpandableListView上,ExpandableListView繼承自ListView

二.兩種介面卡基類的相關類圖與繼承關係

1>BaseAdapter介面卡相關類圖:圖1和圖2

              

                                                                            圖1    BaseAdapter介面卡相關類圖

             

             

                 

                                                                   圖2    BaseAdapter介面卡相關類圖(續)

2>BaseExpandableListAdapter介面卡相關類圖:圖3

                                                                           圖3  BaseExpandableListAdapter介面卡相關類圖

三.重要類的相關方法建構函式的具體分析

1.ArrayAdapter

補充:

1>資料來源寫法對比

1.用靜態字元陣列常量來給ArrayAdapter 賦值。 優點,直接用陣列寫入,資料量大建議使用。

static final String[] list="...";

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item,list);

2.在程式中給ArrayAdapter 賦值。優點:可以在程式中靈活寫入。

ArrayList<String> list = new ArrayList<String>();

list.add("資料1");

list.add("資料N");

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item,list);

3.使用國際化介面 字元陣列來 給ArrayAdapter 賦值。優點:提供的元件的選項可以國際化。

目錄【res】→【values】→【strings.xml】新增

<string-array name="letter">
  <item>A</item>
  <item>B</item>
  <item>C</item>
  <item>D</item>
</string-array>

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.letter,android.R.layout.my_list_item)//只需要顯示

ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this,android.R.layout.my_list_item,Arrays.asList(getResources().getTextArray(R.array.letter)))//允許動態增刪

2>什麼情況使用ArrayAdapter,什麼時候使用BaseAdapter

當數量較多,比如超過100條或頻繁動態增減時使用arrayadapter可以方便控制ui,

如果僅僅為了顯示則使用baseadapter更節省資源

 

fromhttps://www.cnblogs.com/bravestarrhu/archive/2012/05/04/2483179.html