Update data

To understand how to update data, we'll take a look at the TitleEditor class. Because it's small, looking at it in its entirety is instructive. Relatively few lines are needed to manipulate the content provider, and most of the function connects the user's clicks to changes in the content provider. The user interaction uses basic manipulations of graphic elements, which were briefly introduced in Chapter 4 and will be fully discussed in Chapter 10 and subsequent chapters. The rest of this section prints the TitleEditor class in blocks, following each block with explanations.

public class TitleEditor extends Activity implements View.OnClickListener {

/** An array of the columns we are interested in. */ private static final String[] PROJECTION = new String[] { NotePad.Notes._ID, // 0

NotePad.Notes.TITLE, // 1

private static final int COLUMN_INDEX_TITLE = 1;

/** Cursor providing access to the note whose title we are editing. */ private Cursor mCursor;

/** The EditText field from our UI. Used to extract the text when done. */ private EditText mText;

/** The content URI to the note that's being edited. */ private Uri mUri;

This first section of the TitleEditor Activity class sets up all of its private data. The following private variables are declared:


Used by the managedQuery function to describe the columns to return in the query, as shown in the previous section.


Defines the number of the column, in the order returned by the query, from which the title must be pulled. The numbers start at 0, so the value of 1 shown is the index of the TITLE within the PROJECTION string.


Holds the URI of the note whose title we're going to edit. An example URI might be content://com.google.provider.NotePad/notes/2.


The cursor that holds the results of the query. mText

The EditText field on the form.

Next, the Activity's onCreate method sets up the Activity: ^Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);


// Get the uri of the note whose title we want to edit mUri = getIntent().getData();

// Get a cursor to access the note mCursor = managedQuery(mUri, PROJECTION, null, null, null);©

// Set up click handlers for the text field and button mText = (EditText) this.findViewById(R.id.title); mText.setOnClickListener(this);

Button b = (Button) findViewById(R.id.ok); b.setOnClickListener(this);

Here are some of the highlights of the code:

O Finds the ContentView in the res/layout/title_editor.xml layout file, using the setContentView method.

© Runs the managedQuery method to load results into a Cursor.

© Sets click handlers for both the button and the text box. This will direct any clicks on the button or the text box to the onClick method, which we'll see shortly.

When onCreate finishes, the onResume method is called. This method pulls the current value of the note title from the cursor and assigns it to the value of the text box: @Override protected void onResume() { super.onResume();

// Initialize the text with the title column from the cursor if (mCursor != null) { mCursor.moveToFirst();


The onPause method is where the application writes the data back to the database. In other words, NotePad follows the typical Android practice of saving up writes until the application is suspended. We'll see soon where this method is called: @Override protected void onPause() { super.onPause();

// Write the title back to the note ContentValues values = new ContentValues(); values.put(Notes.TITLE, mText.getText().toString()); getContentResolver().update(mUri, values, null, null);

Here are some of the highlights of the code:

O Creates a new ContentValues object to hold the set of values to pass to the ContentResolver.

© Puts the column name and the new value of the column in the values object.

© Stores the updated value by creating a ContentResolver and passing the URI and new vales to its update method.

The last method in TitleEditor is the common callback for handling user clicks, named onClick:

public void onClick(View v) {

// When the user clicks, just finish this activity. // onPause will be called, and we save our data there. finish();

The comment describes what is going on pretty well. Once the user clicks either the OK button or the text box within the dialog box, the Activity calls the finish method. That method calls onPause, which writes the contents of the dialog box back to the database, as we showed earlier.

Was this article helpful?

0 0

Post a comment