## 运用场景
主要用于在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);
}
}
}
~~~