Login     Sign Up
Cyril Sermon (@admin)
9 months ago

Using the Provider

You can now update the Earthquake Activity to use the Earthquake Provider to store quakes, and use

them to populate the List View.

1. Within the Earthquake Activity, update the addNewQuake method. It should use the application’s Content Resolver to insert each new earthquake into the provider. Move the existing array control logic into a separate addQuakeToArray method.

private void addNewQuake(Quake _quake) {
 ContentResolver cr = getContentResolver();
 // Construct a where clause to make sure we don’t already have this 
 // earthquake in the provider.
 String w = EarthquakeProvider.KEY_DATE + “ = “ + 
 // If the earthquake is new, insert it into the provider.
 Cursor c = cr.query(EarthquakeProvider.CONTENT_URI, 
 null, w, null, null);
 int dbCount = c.getCount();
 if (dbCount > 0) {
 ContentValues values = new ContentValues();
 values.put(EarthquakeProvider.KEY_DETAILS, _quake.getDetails());
 double lat = _quake.getLocation().getLatitude();
 double lng = _quake.getLocation().getLongitude();
 values.put(EarthquakeProvider.KEY_LOCATION_LAT, lat);
 values.put(EarthquakeProvider.KEY_LOCATION_LNG, lng);
 values.put(EarthquakeProvider.KEY_LINK, _quake.getLink());
 cr.insert(EarthquakeProvider.CONTENT_URI, values);
private void addQuakeToArray(Quake _quake) {
 if (_quake.getMagnitude() > minimumMagnitude) {
 // Add the new quake to our list of earthquakes.
 // Notify the array adapter of a change.

2. Create a new loadQuakesFromProvider method that loads all the earthquakes from the Earthquake Provider and inserts them into the array list using the addQuakeToArray method created in Step 1.

private void loadQuakesFromProvider() {
// Clear the existing earthquake array
ContentResolver cr = getContentResolver();
 // Return all the saved earthquakes
 Cursor c = cr.query(EarthquakeProvider.CONTENT_URI, 
 null, null, null, null);
 if (c.moveToFirst())
 do { 
 // Extract the quake details.
 Long datems = c.getLong(EarthquakeProvider.DATE_COLUMN);
 String details;
 details = c.getString(EarthquakeProvider.DETAILS_COLUMN);
 Float lat = c.getFloat(EarthquakeProvider.LATITUDE_COLUMN);
 Float lng = c.getFloat(EarthquakeProvider.LONGITUDE_COLUMN);
 Double mag = c.getDouble(EarthquakeProvider.MAGNITUDE_COLUMN);
 String link = c.getString(EarthquakeProvider.LINK_COLUMN);
 Location location = new Location(“dummy”);
 Date date = new Date(datems);
 Quake q = new Quake(date, details, location, mag, link);
 } while(c.moveToNext());

3. Call loadQuakesFromProvider from onCreate to initialize the Earthquake List View at start-up.

public void onCreate(Bundle icicle) {
 earthquakeListView = 
 earthquakeListView.setOnItemClickListener(new OnItemClickListener() {
 public void onItemClick(AdapterView _av, View _v, 
 int _index, long arg3) {
 selectedQuake = earthquakes.get(_index);
 int layoutID = android.R.layout.simple_list_item_1;
 aa = new ArrayAdapter<Quake>(this, layoutID , earthquakes);

4. Finally, make a change to the refreshEarthquakes method so that it loads the saved earthquakes from the provider after clearing the array, but before adding any new quakes received.

private void refreshEarthquakes() {
 [ ... exiting refreshEarthquakes method ... ]
 // Clear the old earthquakes
 [ ... exiting refreshEarthquakes method ... ]


In this chapter, you learned how to add a persistence layer to your applications.

Starting with the ability to save the Activity instance data between sessions using the save and

restore instance state handlers, you were then introduced to Shared Preferences. You used them to

save instance values and user preferences that can be used across your application components.

Android provides a fully featured SQLite RDBMS to all applications. This small, effi cient, and robust

database lets you create relational databases to persist application data. Using Content Providers, you

learned how to share private data, particularly databases, across application boundaries.

All database and Content Provider queries are returned as Cursors; you learned how to perform queries and extract data from the resulting Cursor objects.

Along the way, you also learned to:

Save and load fi les directly to and from the underlying fi lesystem.

Include static fi les as external project resources.

Create new SQLite databases.

Interact with databases to insert, update, and delete rows.

Use the native Content Providers included with Android to access and manage native data like

media and contacts.

With a solid foundation in the fundamentals of Android development, the remainder of this book will

investigate some of the more interesting optional Android features.

Starting in the next chapter, you’ll be introduced to the geographic APIs. Android offers a rich suite

of geographical functionality including location-based services (such as GPS), forward and reverse

geocoding, as well as a fully integrated Google Maps implementation. Using Google Maps, you can

create map-based Activities that feature annotations to develop native map-mashup style applications.