## 運用場景
主要用于在ListView或者GridView等中動態顯示數據庫中創建的數據,功能與AdapterView相似。
## 實現步驟
### 繼承CurosrAdapter
繼承CurosrAdapter后需要重寫兩個方法,分別是:
- public View newView(Context context, Cursor cursor, ViewGroup parent)
- public void bindView(View view, Context context, Cursor cursor)
newView方法用于實現創建新的View,將前文創建的Item樣式在這里擴展顯示。
bindView方法用于實現在新創建的View中將數據庫中對應的數據顯示到對應的id控件上
~~~
public class TableCursorAdapter extends CursorAdapter {
XinGangViewHelper mViewHelper;
public TableCursorAdapter(Context context, Cursor c, XinGangViewHelper view) {
super(context, c, 0);
mViewHelper = view;
}
public View newView(Context context, Cursor cursor, ViewGroup viewgroup) {
LinearLayout parent = new LinearLayout(context);
parent.addView(mViewHelper.getListView(context), new LinearLayout.LayoutParams(-1, 120));
return parent;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
for (String field : mViewHelper.getFields()) {
TextView tv = view.findViewWithTag(field);
tv.setText(cursor.getString(cursor.getColumnIndex(field)));
tv.setTextSize(13);
}
}
}
~~~