[Android] Adapter

Adapter 將資料轉換為元件可以接受的格式,例如我們可以將資料庫的資料顯示在ListView類型的元件或者是下拉式選單Spinner這種類型的元件。

Adapter分為底下幾種類型,用於連接不同形式的資料來源。
ArrayAdapter<T>A concrete BaseAdapter that is backed by an array of arbitrary objects. 
BaseAdapterCommon base class of common implementation for an Adapter that can be used in both ListView (by implementing the specialized ListAdapter interface) and Spinner (by implementing the specialized SpinnerAdapter interface). 
CursorAdapterStatic library support version of the framework's CursorAdapter
HeaderViewListAdapterListAdapter used when a ListView has header views. 
ListAdapterExtended Adapter that is the bridge between a ListView and the data that backs the list. 
ResourceCursorAdapterStatic library support version of the framework's ResourceCursorAdapter
SimpleAdapterAn easy adapter to map static data to views defined in an XML file. 
SimpleCursorAdapterStatic library support version of the framework's SimpleCursorAdapter
SpinnerAdapterExtended Adapter that is the bridge between a Spinner and its data. 
WrapperListAdapterList adapter that wraps another list adapter. 


使用範例:

ArrayAdapter<String> Adapter_note = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, note_array);

setListAdapter(Adapter_note); //Bind to Adapter

要先New一個ArrayAdapter物件,輸入三個參數
1. this 表示目前的Activity
2. android.R.layout.simple_list_item_1 要轉換成的格式
3. note_array 資料來源


透過 getListView().setEmptyView(findViewById(R.id.empty)); 可以顯示當List為空時要顯示的內容。

沒有留言:

張貼留言