Step 9

Fill in the body of the onCreate() method for NoteEdit.

This will set the title of our new Activity to say "Edit Note" (one of the strings defined in strings.xml). It will also set the content view to use our note edit.xml layout file. We can then grab handles to the title and body text edit views, and the confirm button, so that our class can use them to set and get the note title and body, and attach an event to the confirm button for when it is pressed by the user.

We can then unbundle the values that were passed in to the Activity with the extras Bundle attached to the calling Intent. We'll use them to pre-populate the title and body text edit views so that the user can edit them. Then we will grab and store the mRowId so we can keep track of what note the user is editing.

1. Inside onCreate() , set up the layout:

setContentView(R.layout.note edit);

2. Find the edit and button components we need:

These are found by the IDs associated to them in the R class, and need to be cast to the right type of View (EditText for the two text views, and Button for the confirm button):

mTitleText = (EditText) findViewById(R.id.title);

mBodyText = (EditText) findViewById(R.id.body);

Button confirmButton = (Button) findViewById(R.id.confirm);

Note that mTitleText and mBodyText are member fields (you need to declare them at the top of the class definition).

3. At the top of the class, declare a Long mRowId private field to store the current mRowId being edited (if any).

4. Continuing inside onCreate() , add code to initialize the title, body and mRowId from the extras Bundle in the Intent (if it is present):

mRowId = null;

Bundle extras = getIntent().getExtras(); if (extras != null) {

String title = extras.getString(NotesDbAdapter.KEY_TITLE); String body = extras.getString(NotesDbAdapter.KEY_BODY); mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);

mTitleText.setText(title);

mBodyText.setText(body);

o We are pulling the title and body out of the extras Bundle that was set from the Intent invocation. o We also null-protect the text field setting (i.e., we don't want to set the text fields to null accidentally). 5. Create an onClickListener() for the button:

Listeners can be one of the more confusing aspects of UI implementation, but what we are trying to achieve in this case is simple. We want an onClick() method to be called when the user presses the confirm button, and use that to do some work and return the values of the edited note to the Intent caller. We do this using something called an anonymous inner class. This is a bit confusing to look at unless you have seen them before, but all you really need to take away from this is that you can refer to this code in the future to see how to create a listener and attach it to a button. (Listeners are a common idiom in Java development, particularly for user interfaces.) Here's the empty listener:

confirmButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) { }

0 0

Post a comment