在CursorAdapter里面,cursor记录的位置有系统管理,使用的时候将它当作只含有一个记录的对象。用户只需要直接用。
如果自己写一个类继承CursorAdapter,一般都要有private LayoutInflater layoutInflater;这个私有成员,而且这个私有成员在构造函数的时候要获得值。此外需要重写void setView(View view, Cursor cursor)、void bindView(View view, Context context, Cursor cursor)、View newView(Context context, Cursor cursor, ViewGroup parent),构造函数也要写出来,例子:
public class MyCursor extends CursorAdapter { private LayoutInflater layoutInflater; private String type = ""; public MyCursor(Context context, Cursor c, boolean autoRequery, String type) { super(context, c, autoRequery); this.type = type; layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public void bindView(View view, Context context, Cursor cursor) { setChildView(view, cursor); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { View view = layoutInflater.inflate(R.layout.mycursro, null); setChildView(view, cursor); return view; } public void setChildView(View view, Cursor cursor) { TextView IE = (TextView)view.findViewById(R.id4.IE); TextView specific = (TextView)view.findViewById(R.id4.specific); TextView money = (TextView)view.findViewById(R.id4.money); TextView account_type = (TextView)view.findViewById(R.id4.account_type); TextView time = (TextView)view.findViewById(R.id4.time); IE.setText("收支类别:" + cursor.getString(cursor.getColumnIndex("incomeExpense"))); specific.setText("具体类别:" + cursor.getString(cursor.getColumnIndex("type")) + "-" + cursor.getString(cursor.getColumnIndex("smalltype"))); money.setText("金额:" + cursor.getString(cursor.getColumnIndex("transactionAmount"))); //如果是预算就加上预算时间 if("Template".equals(type)) { time.setVisibility(View.GONE); account_type.setText("账户类别:" + cursor.getString(cursor.getColumnIndex("userID"))); } else if("report".equals(type)){ time.setText("记账时间:" + cursor.getString(cursor.getColumnIndex("accountTime"))); account_type.setText("账户类别:" + cursor.getString(cursor.getColumnIndex("userType"))); } else { time.setVisibility(View.VISIBLE); time.setText("预算时间:" + cursor.getString(cursor.getColumnIndex("budgetTime"))); account_type.setText("账户类别:" + cursor.getString(cursor.getColumnIndex("userID"))); } } }
SimpleCursorAdapter类构造函数:
public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)
Parameters
context The context where the ListView associated with this SimpleListItemFactory is running
layout resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"
c The database cursor. Can be null if the cursor is not available yet.
from A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.
to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet.
SimpleAdapter
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
Parameters
context The context where the View associated with this SimpleAdapter is running
data A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
resource Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
from A list of column names that will be added to the Map associated with each item.
to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
每次改变都应用各自的Adapter对象调用notifyDataSetChanged()来通知该对象已经变化,重新刷新列表。