Reputation: 281
I have a listView with checkboxes, that use a cursor adapter. For some reason when I scroll the header (pageOrder==3) moves all over the place. It doesn't stay on top. it will move around to position 13, 2, or 3. any ideas?
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;
}
}
Upvotes: 2
Views: 1218
Reputation: 86948
Sounds like your are having trouble with the View recycler. Try letting the CursorAdapter know that there is more than one layout with these built-in methods:
@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.
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.
Upvotes: 2