0

我将 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 而崩溃。

4

2 回答 2

1

您必须实现自定义适配器并覆盖getItemViewTypegetViewTypeCount. getViewTypeCount必须返回 2(一个视图用于普通行,一个用于分隔符)。getItemViewType应该返回一个 int 来确定视图的类型。会getView收到两次null convertView,一次为普通行,一次为分隔符

于 2013-06-06T08:49:12.917 回答
0

创建您自己的适配器,它将检查要显示的行是否为“数据”并从底层 CursorAdapter 或“分隔符”获取它并提供不同的视图。

于 2013-06-06T08:48:10.157 回答