Step 8

Define the saveState() method to put the data out to the database.

private void saveState() {

String title = mTitleText.getText().toString(); String body = mBodyText.getText().toString(); if (mRowId == null) {

long id = mDbHelper.createNote(title, body); if (id > 0) {

mDbHelper.updateNote(mRowId, title, body);

Note that we capture the return value from createNote() and if a valid row ID is returned, we store it in the mRowId field so that we can update the note in future rather than create a new one (which otherwise might happen if the life-cycle events are triggered).

0 0

Post a comment