我认为描述我的错误的更简单方法是在视频中显示它。但一般的想法是我使用数据库来跟踪复选框列表中的哪些项目被选中。每当我单击一个项目并激活该列表的操作模式时,它会立即忘记这些更改,尽管它们已经进入数据库。更糟糕的是,当多个项目受到这个错误的影响时,它们最终形成了一种奇怪的循环逻辑,其中数据库中的一个项目将被选中,另一个将成为未选中,并且两个将继续循环由于复选框在选中状态和未选中状态之间不断循环并因此触发复选框的onCheckedChangedListener
. 里面没有代码onListItemClick()
这应该会导致这些更改发生在列表级别或数据库级别,并且没有代码会重新加载 CursorLoader 或操作所选项目onPrepareActionMode
。onCreateActionMode
谁能帮我弄清楚这是怎么回事?(我的实现可以在视频中找到,但我也在下面包含了它。)
public class TaskCursorAdapter extends ResourceCursorAdapter{
public View newView(Context context, Cursor cursor, ViewGroup parent){
final LayoutInflater inflater = LayoutInflater.from(context);
final long assignedID = cursor.getInt(cursor.getColumnIndex(NagTasksDatabaseHelper.ID));
View v = inflater.inflate(layout, parent, false);
CheckBox taskCheckBox = (CheckBox)v.findViewById(R.id.taskCheckBox);
TextView taskText = (TextView)v.findViewById(R.id.taskTitle);
TextView taskNote = (TextView)v.findViewById(R.id.taskNote);
if (cursor.getInt(cursor.getColumnIndex(NagTasksDatabaseHelper.CHECKED))==1){
taskText.setPaintFlags(taskText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
taskNote.setPaintFlags(taskNote.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
taskCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Current method:
AlertDialog.Builder builder = new AlertDialog.Builder(buttonView.getContext());
builder.setCancelable(true);
Log.i("TaskCursorAdapter", "CheckBox assignedID="+assignedID);
if (assignedID!=-1) {
NagTasksDatabaseHelper helper = new NagTasksDatabaseHelper(buttonView.getContext());
if (isChecked) {
helper.checkOffTask(assignedID);
//builder.setMessage("Item "+assignedID+" checked");
} else {
helper.uncheckTask(assignedID);
//builder.setMessage("Item "+assignedID+" unchecked");
}
helper.close();
} else {
builder.setMessage("No ID found. Try something else.");
builder.create().show();
}
}
});
return v;
}
public void bindView(View v, Context context, Cursor cursor){
CheckBox taskCheckBox = (CheckBox)v.findViewById(R.id.taskCheckBox);
TextView taskText = (TextView)v.findViewById(R.id.taskTitle);
TextView taskNote = (TextView)v.findViewById(R.id.taskNote);
taskCheckBox.setChecked(cursor.getInt(cursor.getColumnIndex(NagTasksDatabaseHelper.CHECKED))==1);
//final long assignedID = v.getId();
//v.setClickable(true);
//LinearLayout holder = (LinearLayout)v.findViewById(R.id.container);
//holder.setClickable(true);
//IdFromDb = c.getInt(c.getColumnIndex(NagTasksDatabaseHelper.ID));
taskText.setText(cursor.getString(cursor.getColumnIndex(NagTasksDatabaseHelper.TASK)));
taskNote.setText(cursor.getString(cursor.getColumnIndex(NagTasksDatabaseHelper.NOTE)));
//isCheckedFromDb = c.getInt(c.getColumnIndex(NagTasksDatabaseHelper.CHECKED));
//cursor = c;
}
public class TaskListFragment extends SherlockListFragment implements ActionMode.Callback {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu_single_choice, menu);
mode.setTitle(R.string.activeActionMode);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onListItemClick(ListView l, View v, int position, long id){
//super.onListItemClick(l, v, position, id);
Log.i("NagTaskListFragment", "User clicked on "+id);
if (mActionMode == null) {
idForActionMode=id;
mActionMode = getSherlockActivity().startActionMode(this);
}
if (l.getSelectedView() == v) {
v.setSelected(false);
mActionMode.finish();
mActionMode = null;
idForActionMode = -1;
} else if (l.getSelectedView() != null) {
l.getSelectedView().setSelected(false);
v.setSelected(true);
idForActionMode = id;
//onPrepareActionMode(mActionMode, mActionMode.getMenu());
//Log.i("NagTaskListFragment", "selectedItemID = "+l.getSelectedItemId());
} else {
v.setSelected(true);
idForActionMode = id;
//Log.i("NagTaskListFragment", "selectedItemID = "+l.getSelectedItemId());
}
}