Next, open the Notepadvl class in the source. In the following steps, we are going to alter this class to become a list adapter and display our notes, and also allow us to add new notes.

Notepadvl will inherit from a subclass of Activity called a ListActivity, which has extra functionality to accommodate the kinds of things you might want to do with a list, for example: displaying an arbitrary number of list items in rows on the screen, moving through the list items, and allowing them to be selected.

Take a look through the existing code in Notepadvl class. There is a currently an unused private field called mNoteNumber that we will use to create numbered note titles.

There are also three override methods defined: onCreate, onCreateOptionsMenu and onOptionsItemSelected; we need to fill these out:

• onCreate() is called when the activity is started — it is a little like the "main" method for an Activity. We use this to set up resources and state for the activity when it is running.

• onCreateOptionsMenu() is used to populate the menu for the Activity. This is shown when the user hits the menu button, and has a list of options they can select (like "Create Note").

• onOptionsItemSelected() is the other half of the menu equation, it is used to handle events generated from the menu (e.g., when the user selects the "Create Note" item).

0 0

Post a comment