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: