0

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

1 回答 1

0

您发布的代码不会改变光标在其他任何地方的位置。

但是,如果cursor引用绑定到 ListView 的 CursorAdapter 中使用的相同 Cursor,则在绘制 Dialog 时,它可能会掩盖 ListView 的一部分,从而迫使 ListView 重绘。这肯定会改变立场。

解决方案:要么moveToPosition()在 OnClickListener 中再次使用,要么获取一个独立的 Cursor。

于 2012-12-27T18:05:16.477 回答