我将 ListView 与 CursorAdapter 一起使用。这是伟大的。但是客户在视图之间的列表中订购了其他信息:
我怎样才能提供这个?
更新:
public class OutlaysCursorAdapter extends CursorAdapter {
private static int NUMBER;
private static int ID;
private static int NOTE;
private static int TYPE;
private static int SUM;
private static int DATE;
private static int ODOMETER;
private static int UNREAD;
private static int FUTURE;
private static int INPUT_TYPE;
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
return super.getView(arg0, arg1, arg2);
}
@Override
public View newView(Context paramContext, Cursor cursor, ViewGroup paramViewGroup) {
if(cursor.getInt(TYPE)==0){
return new OutlaySeparatorView(paramContext);
} else {
return new OutlayItemView(paramContext);
}
}
@Override
public void bindView(View paramView, Context paramContext, Cursor cursor) {
long date = cursor.getLong(DATE);
int type = cursor.getInt(TYPE);
float sum = cursor.getFloat(SUM);
if(type == 0){
((OutlaySeparatorView)paramView).setData(date, (int)sum);
} else {
OutlayItemView view = (OutlayItemView) paramView;
Outlay outlay = new Outlay();
outlay.setNumber(cursor.getInt(NUMBER));
outlay.setId(cursor.getInt(ID));
outlay.setDate(date);
outlay.setNote(cursor.getString(NOTE));
outlay.setType(type);
outlay.setSum(sum);
outlay.setOdometer(cursor.getInt(ODOMETER));
outlay.setUnread(cursor.getInt(UNREAD) == 1);
outlay.setFuture(cursor.getInt(FUTURE) == 1);
outlay.setInputType(cursor.getString(INPUT_TYPE));
view.setOutlay(outlay);
}
}
public OutlaysCursorAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
initialize(context, c);
}
public OutlaysCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
initialize(context, c);
}
private void initialize(Context context, Cursor c){
NUMBER = c.getColumnIndex("countNum");
ID = c.getColumnIndex("id");
SUM = c.getColumnIndex("sum");
NOTE = c.getColumnIndex("note");
TYPE = c.getColumnIndex("type");
DATE = c.getColumnIndex("date");
ODOMETER = c.getColumnIndex("odometer");
UNREAD = c.getColumnIndex("unread");
FUTURE = c.getColumnIndex("future");
INPUT_TYPE = c.getColumnIndex("input_type");
}
@Override
public int getViewTypeCount() {
return 2;
}
}
这段代码在第一次显示时运行良好,但是当我添加新行并调用 getCursor().requery() 时,这段代码因 ClassCastException 而崩溃。