0

我正在尝试使用带有插入、更新和删除功能的句柄数据库,例如记事本。我在取消数据时遇到问题。在正常情况下按下确认按钮,它将被保存到 sqlite 并显示在列表视图中。如何通过返回键或更多按钮事件进行取消事件?我希望我的按钮和返回键取消数据,但它继续保存......

        public static int numTitle = 1;
public static String curDate = "";
private EditText mTitleText;
private EditText mBodyText;
private Long mRowId;
private NotesDbAdapter mDbHelper;
private TextView mDateText;
private boolean isOnBackeyPressed;
public SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();

    setContentView(R.layout.note_edit);
    setTitle(R.string.edit_note);

    mTitleText = (EditText) findViewById(R.id.etTitle_NE);
    mBodyText = (EditText) findViewById(R.id.etBody_NE);
    mDateText = (TextView) findViewById(R.id.tvDate_NE);

    long msTime = System.currentTimeMillis();
    Date curDateTime = new Date(msTime);

    SimpleDateFormat formatter = new SimpleDateFormat("d'/'M'/'y");
    curDate = formatter.format(curDateTime);

    mDateText.setText("" + curDate);

    Button confirmButton = (Button) findViewById(R.id.bSave_NE);
    Button cancelButton = (Button) findViewById(R.id.bCancel_NE);
    Button deleteButton = (Button) findViewById(R.id.bDelete_NE);

    mRowId = (savedInstanceState == null) ? null
            : (Long) savedInstanceState
                    .getSerializable(NotesDbAdapter.KEY_ROWID);
    if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                : null;
    }

    populateFields();

    confirmButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            setResult(RESULT_OK);
            Toast.makeText(NoteEdit.this, "Saved", Toast.LENGTH_SHORT)
                    .show();
            finish();
        }

    });
    deleteButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mDbHelper.deleteNote(mRowId);
            Toast.makeText(NoteEdit.this, "Deleted", Toast.LENGTH_SHORT)
                    .show();
            finish();
        }
    });
    cancelButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            boolean diditwork = true;

            try {
                db.beginTransaction();
                populateFields();
                 db.setTransactionSuccessful();
            } catch (SQLException e) {
                diditwork = false;
            } finally {
                db.endTransaction();
                if (diditwork) {
                    Toast.makeText(NoteEdit.this, "Canceled",
                            Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
}

private void populateFields() {
    if (mRowId != null) {
        Cursor note = mDbHelper.fetchNote(mRowId);
        startManagingCursor(note);
        mTitleText.setText(note.getString(note
                .getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
        mBodyText.setText(note.getString(note
                .getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    saveState();
    outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
}

public void onBackPressed() {
    super.onBackPressed();
    isOnBackeyPressed = true;
    finish();
}

@Override
protected void onPause() {
    super.onPause();
    if (!isOnBackeyPressed)
        saveState();
}

@Override
protected void onResume() {
    super.onResume();
    populateFields();
}

private void saveState() {
    String title = mTitleText.getText().toString();
    String body = mBodyText.getText().toString();

    if (mRowId == null) {
        long id = mDbHelper.createNote(title, body, curDate);
        if (id > 0) {
            mRowId = id;
        }
    } else {
        mDbHelper.updateNote(mRowId, title, body, curDate);
    }
}`enter code here`
4

1 回答 1

1

这是在 SQLite 中使用事务的简短示例(db 是下面的 SQLiteDatabase 实例):

try {
    db.beginTransaction();
    // your sql stuff
    db.setTransactionSuccessful();
} catch(SQLException e) {
    // do some error handling
} finally {
   db.endTransaction();
}

请注意,无论您选择用 throws 和 exception 替换“//your sql stuff”的任何方法,这一点都很重要。使用insertOrThrow() 或者如果您需要更大的灵活性 SQLiteStatement 实例(它们的 .execute 方法总是在错误时抛出异常)。

请注意,您不需要显式回滚。如果您在db.endTransaction()没有.setTransactionSuccessful()它的情况下调用,它将自动回滚。

请记住始终将 setTransactionSuccessful 放在最后一个 SQLException 抛出方法之后:)

您可以使用另一个 catch 块轻松扩展它,以捕获网络超时的异常。

现在您可以做的是取消或后退按钮可以像此示例中的异常一样起作用,并且将自动调用 endTransaction ,这将导致您的数据回滚

于 2013-06-24T20:45:55.220 回答