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.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Responses

  • jan
    How to display contact name and number using simple cursor adaptor?
    7 years ago

Post a comment