简体   繁体   English

当列表开始滚动时,使用 CursorAdapter 的 Android ListView 混乱

[英]Android ListView using CursorAdapter messes up when list starts scrolling

I have a listView with checkboxes, that use a cursor adapter.我有一个带有复选框的 listView,它使用光标适配器。 For some reason when I scroll the header (pageOrder==3) moves all over the place.出于某种原因,当我滚动标题 (pageOrder==3) 时会到处移动。 It doesn't stay on top.它不会停留在顶部。 it will move around to position 13, 2, or 3. any ideas?它将移动到位置 13、2 或 3。有什么想法吗?

public class ExamCursorAdapter extends CursorAdapter {

private LayoutInflater inflater;
private int pageIndex;
private int pageTitleIndex;
private int pageOrderIndex;
private Context context;
private String assessmentId;

static class ViewHolder{
    protected TextView textViewTitle;
    protected TextView textViewHeader;
    protected TextView textViewCheckBox;
    protected CheckBox checkbox;
    protected int pageOrder;
    protected int pageId;
}

public ExamCursorAdapter(Context context, Cursor cursor, String assessmentId) {
    super(context, cursor, 0);
    this.context = context;
    this.assessmentId = assessmentId;
    pageIndex = cursor.getColumnIndex(PagesTable.COLUMN_ID);
    pageTitleIndex = cursor.getColumnIndex(PagesTable.COLUMN_TITLE);
    pageOrderIndex = cursor.getColumnIndex(PagesTable.COLUMN_ORDER);
    inflater = LayoutInflater.from(context);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.textViewTitle.setText(cursor.getString(pageTitleIndex)); 
    holder.pageOrder = cursor.getInt(pageOrderIndex);

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    ViewHolder holder = new ViewHolder();

    View view = null;
    holder.pageId = cursor.getInt(pageIndex);
    holder.pageOrder = cursor.getInt(pageOrderIndex);

    if(holder.pageOrder == 3) {
        view = inflater.inflate(R.layout.list_adapter_header_exam, null);
        holder.textViewTitle = (TextView) view.findViewById(R.id.adapter_header_textview_exam_column1);
        holder.textViewCheckBox = (TextView) view.findViewById(R.id.adapter_header_textview_exam_column2);
        holder.textViewCheckBox.setText("Complete");
        view.setFocusable(true);
    } else {
        view = inflater.inflate(R.layout.exam_row, null);
        holder.textViewTitle = (TextView) view.findViewById(R.id.exam_cursor_adapter_textview);
        holder.checkbox = (CheckBox) view.findViewById(R.id.exam_cursor_adapter_check_box);
        holder.checkbox.setClickable(false);    
        String[] projection = { ExamCompleteTable.COLUMN_EXAM_COMPLETE };
        StringBuilder sb = new StringBuilder();
        sb.append(BPContentProvider.EXAM_COMPLETE_URI).append("/assessment/").append(assessmentId);
        sb.append("/page/").append(holder.pageId);
        Uri examCompleteUri = Uri.parse(sb.toString());
        Cursor examCompleteCursor = context.getContentResolver().query(examCompleteUri, projection, null, null, null);
        if (examCompleteCursor.moveToFirst()) {
            int examComplete = examCompleteCursor.getInt(examCompleteCursor.getColumnIndexOrThrow(ExamCompleteTable.COLUMN_EXAM_COMPLETE));
            if(examComplete == 1) {
                holder.checkbox.setChecked(true);
            }
        } 
        examCompleteCursor.close();
    }
    view.setTag(holder);
    return view;
}


}

Sounds like your are having trouble with the View recycler.听起来您在使用 View 回收器时遇到了问题。 Try letting the CursorAdapter know that there is more than one layout with these built-in methods:尝试让 CursorAdapter 知道这些内置方法不止一种布局:

@Override
public int getItemViewType(int position) {
    Cursor cursor = getCursor();
    cursor.moveToPosition(position);
    return (cursor.getInt(pageOrderIndex) == 3 ? 0 : 1);
}

@Override
public int getViewTypeCount() {
    return 2;
}

Also this data looks like it changes depending on the content in each row:此外,此数据看起来会根据每行中的内容而变化:

String[] projection = { ExamCompleteTable.COLUMN_EXAM_COMPLETE };
StringBuilder sb = new StringBuilder();
sb.append(BPContentProvider.EXAM_COMPLETE_URI).append("/assessment/").append(assessmentId);
sb.append("/page/").append(holder.pageId);
Uri examCompleteUri = Uri.parse(sb.toString());
Cursor examCompleteCursor = context.getContentResolver().query(examCompleteUri, projection, null, null, null);
if (examCompleteCursor.moveToFirst()) {
    int examComplete = examCompleteCursor.getInt(examCompleteCursor.getColumnIndexOrThrow(ExamCompleteTable.COLUMN_EXAM_COMPLETE));
    if(examComplete == 1) {
        holder.checkbox.setChecked(true);
    }
} 
examCompleteCursor.close();

So this code needs to be in bindView() (or getView() ) to change with the data in each row.因此,此代码需要在bindView() (或getView() )中以随每一行中的数据而变化。

As a tip, if examComplete stays constant while this Activity is in the foreground you can save these integers in a SparseIntArray or List<Integer> to speed things up.作为提示,如果该 Activity 在前台时examComplete保持不变,您可以将这些整数保存在SparseIntArrayList<Integer>以加快速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM