Adapter是串連後端資料和最上層顯示的適配器介面,是資料和UI(View)之間一個重要的紐帶。在常見的View(ListView,GridView)等地方都需要用到Adapter。如直觀的表達了Data、Adapter、View三者的關係:
BaseAdapter是一個abstrace class要使用時必須重寫他的方法,注意這些方法都是抽象方法,分別生命在Adapter abstrace class中。分別是:
public int getCount()返回該Adapter中裝載的資源的數量
public Object getItem(int position)從data set中取出指定poistion處的資料
public long getItemId(int position)return position處的item所在的row id
public View getView(int position, View convertView, ViewGroup parent)
position:data set 中position處的資料,this data we want to view.
convertView:使用之前用convertView ? null判斷,if can't covert to a view來裝載資料然後顯示的話,就需要建立一個新的view了;了;來裝載資料.
parent: as the convertView's parent view and must attached to parent.
after call this function will return the view with data in the position of the adapter.
點擊時候的響應函數:
public abstract void onItemClick
(AdapterView<?> parent,
View view, int position, long id)
| parent |
The AdapterView where the click happened.發生click事件的那個AdapterView |
| view |
The view within the AdapterView that was clicked (this will be a view provided by the adapter) |
| position |
The position of the view in the adapter. |
| id |
The row id of the item that was clicked. |
ImageSwitcher: