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 + " = " + _quake.getDate().getTime(); // If the earthquake is new, insert it into the provider.

if (cr.query(EarthquakeProvider.CONTENT_URI, null, w, null, null).getCount()==0){ ContentValues values = new ContentValues();

values.put(EarthquakeProvider.KEY_DATE, _quake.getDate().getTime()); 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()); values.put(EarthquakeProvider.KEY_MAGNITUDE, _quake.getMagnitude());

cr.insert(EarthquakeProvider.CONTENT_URI, values); earthquakes.add(_quake);

addQuakeToArray(_quake);

private void addQuakeToArray(Quake _quake) {

if (_quake.getMagnitude() > minimumMagnitude) { // Add the new quake to our list of earthquakes. earthquakes.add(_quake);

// Notify the array adapter of a change. aa.notifyDataSetChanged();

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 earthquakes.clear();

ContentResolver cr = getContentResolver();

// Return all the saved earthquakes

Cursor c = cr.query(EarthquakeProvider.CONTENT_URI, null, null, null, null);

// Extract the quake details.

Long datems = c.getLong(EarthquakeProvider.DATE_COLUMN); String 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");

location.setLongitude(lng);

location.setLatitude(lat);

Date date = new Date(datems);

Quake q = new Quake(date, details, location, mag, link); addQuakeToArray(q); } while(c.moveToNext());

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

©Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

earthquakeListView = (ListView)this.findViewById(R.id.earthquakeListView); earthquakeListView.setOnItemClickListener(new OnItemClickListener() { ©Override public void onItemClick(AdapterView _av, View _v, int _index, long arg3) { selectedQuake = earthquakes.get(_index); showDialog(QUAKE_DIALOG);

int layoutID = android.R.layout.simple_list_item_1;

aa = new ArrayAdapter<Quake>(this, layoutID , earthquakes);

earthquakeListView.setAdapter(aa);

loadQuakesFromProvider();

updateFromPreferences(); refreshEarthquakes();

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() {

// Clear the old earthquakes earthquakes.clear();

loadQuakesFromProvider();

All code snippets in this example are part of the Chapter 7 Todo List 3 project, available for download at Wrox.com.

NATIVE ANDROID CONTENT PROVIDERS

Android exposes several native databases using Content Providers.

You can access these Content Providers directly using the techniques described earlier in this chapter. Alternatively, the android.provider package includes classes that can simplify access to many of the most useful providers, including:

> Browser Use the browser Content Provider to read or modify bookmarks, browser history, or web searches.

> CallLog View or update the call history, including both incoming and outgoing calls, together with missed calls and call details like caller ID and call durations.

> ContactsContract Use the Contacts Contract provider to retrieve, modify, or store your contacts' details. This Content Provider replaces the Contact Content Provider.

> MediaStore The Media Store provides centralized, managed access to the multimedia on your device, including audio, video, and images. You can store your own multimedia within the media store and make it globally available, as shown in Chapter 11.

> Settings You can access the device's preferences using the Settings provider. You can view most system settings and modify some of them. More usefully, the android.provider.Settings class includes a collection of Intent actions that can be used to open the appropriate settings screen to let users modify their own settings.

> UserDictionary Access (or add to) the user defined words added to the dictionary for use in IME predictive text input.

You should use these native Content Providers wherever possible to ensure your application integrates seamlessly with other native and third-party applications.

While a detailed description of how to use each of these helpers is beyond the scope of this chapter, the following sections describe how to use the Media Store and Contacts Contract Content Provider.

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


Post a comment