我创建了一个包含CheckBox和Text小部件的自定义ListView。并使用SimpleCursorAdapter将其绑定到数据库记录。我希望在用户选中CheckBox时实现它,状态Text将更改为“Finished”。如果用户未选中,状态将恢复为“待处理”。
public class SpecialAdapter extends SimpleCursorAdapter {
public ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
private int color_pending = 0xFF666666;
private int color_finished = 0xFFCCCCCC;
private String STATUS_PENDING, STATUS_FINISHED;
private NotesDbAdapter mDbHelper;
//private Context context;
public SpecialAdapter(Context context, int resource, Cursor items, String[] from, int[] to) {
super(context, resource, items, from, to);
//this.context = context;
mDbHelper = new NotesDbAdapter(context);
STATUS_PENDING = context.getString(R.string.note_status_pending);
STATUS_FINISHED = context.getString(R.string.note_status_finished);
for (int i = 0; i < this.getCount(); i++) {
itemChecked.add(i, false); // initializes all items value with false
}
}
private void setTextColorPending(TextView title, TextView content, TextView status, TextView dateTime, CheckBox cBox) {
title.setTextColor(color_pending);
content.setTextColor(color_pending);
status.setTextColor(color_pending);
dateTime.setTextColor(color_pending);
}
private void setTextColorFinished(TextView title, TextView content, TextView status, TextView dateTime, CheckBox cBox) {
title.setTextColor(color_finished);
content.setTextColor(color_finished);
status.setTextColor(color_finished);
dateTime.setTextColor(color_finished);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
final TextView title = (TextView)view.findViewById(R.id.lTitle);
final TextView content = (TextView)view.findViewById(R.id.lContent);
final TextView status = (TextView)view.findViewById(R.id.lStatus);
final TextView dateTime = (TextView)view.findViewById(R.id.lDate);
final CheckBox cBox = (CheckBox)view.findViewById(R.id.list_checkbox);
final Cursor c = getCursor();
c.moveToPosition(position);
int col = c.getColumnIndex(NotesDbAdapter.COL_STATUS);
final boolean isPending = c.getInt(col) == NotesDbAdapter.STATUS_PENDING;
if (isPending) {
setTextColorPending(title, content, status, dateTime, cBox);
cBox.setChecked(false);
}
else {
setTextColorFinished(title, content, status, dateTime, cBox);
cBox.setChecked(true);
}
cBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDbHelper.open();
CheckBox cb = (CheckBox)v.findViewById(R.id.list_checkbox);
if (cb.isChecked()) {
itemChecked.set(position, true);
setTextColorFinished(title, content, status, dateTime, cBox);
int newStatus = NotesDbAdapter.STATUS_FINISHED;
mDbHelper.updateStatus(position, newStatus);
} else if (!cb.isChecked()) {
itemChecked.set(position, false);
setTextColorPending(title, content, status, dateTime, cBox);
int newStatus = NotesDbAdapter.STATUS_PENDING;
mDbHelper.updateStatus(position, newStatus);
}
cBox.setChecked(itemChecked.get(position));
c.requery();
mDbHelper.close();
}
});
return view;
}
@Override
public void setViewText(TextView v, String text) {
if (v.getId() == R.id.lStatus) {
text = NotesDbAdapter.STATUS_PENDING == Integer.parseInt(text) ? STATUS_PENDING : STATUS_FINISHED;
}
v.setText(text);
}
}
这是类SpecialAdapter代码,它从SimpleCursorAdapter扩展而来。在覆盖函数getView - &gt; onClick函数,我更新了数据库上下文,然后调用c.requery()。但它没有按预期工作。
on MainActivity(DroidNotesActivity)代码:
public void listAll() {
cursor = mDbHelper.listAll();
String[] from = new String[] {
NotesDbAdapter.COL_ID,
NotesDbAdapter.COL_TITLE,
NotesDbAdapter.COL_MESSAGE,
NotesDbAdapter.COL_DATE,
NotesDbAdapter.COL_STATUS};
int[] to = new int[] {R.id.hiddenId, R.id.lTitle, R.id.lContent, R.id.lDate, R.id.lStatus};
SpecialAdapter notes = new SpecialAdapter(this, R.layout.note_row, cursor, from, to);
listView.setAdapter(notes);
int numNotes = Integer.parseInt(mDbHelper.count());
if(numNotes == 1) {
tNumNotes.setText(numNotes + " " + getApplicationContext().getString(R.string.note_found));
} else if (numNotes > 0) {
tNumNotes.setText(numNotes + " " + getApplicationContext().getString(R.string.notes_found));
} else {
tNumNotes.setText(getApplicationContext().getString(R.string.no_note_found));
}
}
有没有人可以帮我弄清楚它显示错误的原因。感谢