List adapters

Our example uses aSimpleCursorAdapter to bind a database Cursor into a ListView, and this is a common way to use aListAdapter. Other options exist likeArrayAdapter which can be used to take a List or Array of in-memory data and bind it in to a list as well. Define the fillData() method:

This method uses SimpleCursorAdapter, which takes a database Cursor and binds it to fields provided in the layout. These fields define the row elements of our list (in this case we use the textl field in our notes row.xml layout), so this allows us to easily populate the list with entries from our database.

To do this we have to provide a mapping from the title field in the returned Cursor, to our textl TextView, which is done by defining two arrays: the first a string array with the list of columns to map from (just "title" in this case, from the constant NotesDbAdapter.KEY TITLE) and, the second, an int array containing references to the views that we'll bind the data into (the R. id.textl TextView).

This is a bigger chunk of code, so let's first take a look at it:

private void fillData() {

// Get all of the notes from the database and create the item list

Cursor c = mDbHelper.fetchAllNotes();


String[] from = new String[] { NotesDbAdapter.KEY_TITLE }; int[] to = new int[] { };

// Now create an array adapter and set it to display using our row

SimpleCursorAdapter notes =

new SimpleCursorAdapter(this, R.layout.notes row, c, from, to); setListAdapter(notes);

Here's what we've done:

1. After obtaining the Cursor from mDbHelper.fetchAllNotes() , we use an Activity method called startManagingCursor() that allows Android to take care of the Cursor lifecycle instead of us needing to worry about it. (We will cover the implications of the lifecycle in exercise 3, but for now just know that this allows Android to do some of our resource management work for us.)

2. Then we create a string array in which we declare the column(s) we want (just the title, in this case), and an int array that defines the View(s) to which we'd like to bind the columns (these should be in order, respective to the string array, but here we only have one for each).

3. Next is the SimpleCursorAdapter instantiation. Like many classes in Android, the SimpleCursorAdapter needs a Context in order to do its work, so we pass in this for the context (since subclasses of Activity implement Context). We pass the notes row View we created as the receptacle for the data, the Cursor we just created, and then our arrays.

In the future, remember that the mapping between the from columns and to resources is done using the respective ordering of the two arrays. If we had more columns we wanted to bind, and more Views to bind them in to, we would specify them in order, for example we might use { NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_BODY } and {, } to bind two fields into the row (and we would also need to define text2 in the notes_row.xml, for the body text). This is how you can bind multiple fields into a single row (and get a custom row layout as well).

If you get compiler errors about classes not being found, ctrl-shift-O or (cmd-shift-O on the mac) to organize imports.

0 0

Post a comment