The busy coders guide to android development feb 2009

Page 289

Using a Content Provider

SimpleCursorAdapter,

hand the adapter off to the widget, and you're set – your widget will show the available options. For example, here is a fragment of the onCreate() method from TourListActivity, which gives the user a list of tours to choose from: toursCursor = managedQuery(getIntent().getData(), PROJECTION, null, null, null); list=(ListView)findViewById(android.R.id.list); list.setOnItemClickListener(this); ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.tourlist_item, toursCursor, new String[] {Provider.Tours.TITLE}, new int[] {android.R.id.text1}); list.setAdapter(adapter);

After executing the managedQuery() and getting the Cursor, TourListActivity creates a SimpleCursorAdapter with the following parameters: •

The activity (or other Context) creating the adapter; in this case, the TourListActivity itself

The identifier for a layout to be used for rendering the list entries (R.layout.tourlist_item)

The cursor (toursCursor)

The properties to pull out of the cursor and use for configuring the list entry View instances (TITLE)

The corresponding identifiers of TextView widgets in the list entry layout that those properties should go into (android.R.id.text1)

After that, we put the adapter into the ListView, and we get:

268

This document is licensed for Cheng Po-wen's exclusive use by CommonsWare, LLC


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.