Using the Simple Cursor Adapter

The SimpleCursorAdapter lets you bind a Cursor to a List View, using a custom layout definition to define the layout of each row/item, which is populated by a row's column values.

Construct a Simple Cursor Adapter by passing in the current context, a layout resource, a Cursor, and two arrays: one that contains the names of the columns to be used, and a second (equally-sized) array that has resource IDs for the Views to use to display the contents of the corresponding columns.

Listing 5-25 shows how to construct a Simple Cursor Adapter to display contact information.

LISTING 5-25: Creating a Simple Cursor Adapter Available for download on String uriString = "content://contacts/people/";

Wrox.com Cursor myCursor = managedQuery(Uri.parse(uriString), null, null, null); String[] fromColumns = new String[] {People.NUMBER, People.NAME}; int[] toLayoutIDs = new int[] { R.id.nameTextView, R.id.numberTextView};

SimpleCursorAdapter myAdapter; myAdapter = new SimpleCursorAdapter(this,

R.layout.simplecursorlayout, continues

LISTING 5-25 (continued)

myCursor, fromColumns, toLayoutIDs);

myListView.setAdapter(myAdapter);

The Simple Cursor Adapter was used earlier in this chapter in the Contact Picker example. You'll learn more about Content Providers and Cursors in Chapter 7, where you'll also find more Simple Cursor Adapter examples.

0 0

Post a comment

  • Receive news updates via email from this site