我的数据库中的奇怪错误会在没有任何错误发生时导致更改

时间:2012-07-25 23:20:10

标签: android sql sqlite

我认为描述我的错误的更简单方法是showing it in a video。但一般的想法是我使用数据库来跟踪复选框列表中的哪些项目被检查。每当我点击一个项目并激活该列表的动作模式时,它会立即忘记这些更改,尽管它们已经进入数据库。更糟糕的是当多个项目受到这个bug的影响时,它们最终会形成一个奇怪的循环逻辑,其中数据库中的一个项目将被检查而另一个项目将被取消选中,并且两个项目将在一个周期中继续由于复选框在已检查状态和未检查状态之间不断循环,因此在列表上执行操作,从而触发复选框onCheckedChangedListeneronListItemClick()中没有代码可以导致这些更改在列表级别或数据库级别上发生,并且onPrepareActionModeonCreateActionMode中没有会重新加载CursorLoader或操作的代码选定的项目。任何人都可以帮我找出导致这种情况的原因吗? (我的实现可以在视频中找到,但我也将其包含在下面。)

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());
    }
}

1 个答案:

答案 0 :(得分:0)

所以,我将setChecked()方法移到了newView,这似乎解决了这个问题。我不确定这是否是正确的解决方案,但它现在有效。