ListView与元素之间的额外视图

时间:2013-06-06 08:45:05

标签: android listview android-listview

我将ListView与CursorAdapter一起使用。这很棒。但是客户在视图之间的列表中订购了其他信息:

enter image description here

我该如何提供?

更新:

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崩溃。

2 个答案:

答案 0 :(得分:1)

您必须实施自定义适配器并覆盖getItemViewTypegetViewTypeCountgetViewTypeCount必须返回2(一个View用于正常行,一个用于Separtor)。 getItemViewType应该返回一个int,它将验证View的类型。 getView将收到两次null convertView,一个用于正常行,一个用于分隔符

答案 1 :(得分:0)

创建您自己的适配器,该适配器将检查要显示的行是否为“data”并从基础CursorAdapter或“separator”获取它,并提供不同的视图。