Search code examples
androidcursorandroid-sqliteandroid-dialogfragment

DialogFragment OnClickListener strange Cursor behavior


I am using the code below in a class extending DialogFragment. Before adding the buttons I move a cursor to a certain position (see setDataFromCursor()) The strange thing is, that the position has changed when trying to get the position again in the OnClickListener of the positive button. I added some Log output which proves that. Does anybody know why that happens?

Thanks for your help!

Edit: In the debugger it is visible too. I step forward a few steps and the cursor position changes without calling moveToPosition.

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Log.v(TAG, "onCreateDialog");
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        view = inflater.inflate(R.layout.query_tab_dialog_add_operation,
                null);
        name = (TextView) view.findViewById(R.id.editTextOPName);
        cimclass = (TextView) view.findViewById(R.id.editTextCIMclass);
        setDataFromCursor();
        builder.setView(view)
                .setTitle(
                        R.string.querytabfragment_dialog_editOperation_title)
                .setPositiveButton(R.string.apply,
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                nameStr = name.getText().toString();
                                cimclassStr = cimclass.getText().toString();

                                Log.d(TAG,
                                        "cursor position: "
                                                + cursor.getPosition());
                                int id = cursor.getInt(0);
                                Log.d(TAG, "cursor on item: " + id);
                                // update affected row
                                queryDBHelper.editCIMQuery(id, nameStr,
                                        cimclassStr);
                                Log.d(TAG, "query edited. id: " + id
                                        + " name: " + nameStr
                                        + " cimclass: " + cimclassStr);
                                cursor.requery();
                            }
                        })
                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                EditOperationDialogFragment.this
                                        .getDialog().cancel();

                            }
                        });
        return builder.create();
    }

    private void setDataFromCursor() {
        cursor.moveToPosition(cursorPosition);
        name.setText(cursor.getString(1));
        cimclass.setText(cursor.getString(2));          
    }

Solution

  • The code that you posted doesn't change the Cursor's position anywhere else.

    However, if cursor references the same Cursor used in a CursorAdapter bound to a ListView then when the Dialog is drawn it probably obscures part of a ListView forcing the ListView to redraw. This definitely changes the position.

    Solution: Either use moveToPosition() again in the OnClickListener or fetch an independent Cursor.