14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

}<br />

break;<br />

Chapter 6: Data Storage, Retrieval, and Sharing<br />

+ where + ‘)’ : “”), whereArgs);<br />

default: throw new IllegalArgumentException(“Unknown URI “ + uri);<br />

getContext().getContentResolver().notifyChange(uri, null);<br />

return count;<br />

9. With the Content Provider fi nished, register it in the manifest by creating a new node within the<br />

application tag.<br />

Using the Provider<br />

<br />

You can now update the Earthquake Activity to use the Earthquake Provider to store quakes, and use<br />

them to populate the List View.<br />

1. Within the Earthquake Activity, update the addNewQuake method. It should use the application’s<br />

Content Resolver to insert each new earthquake into the provider. Move the existing<br />

array control logic into a separate addQuakeToArray method.<br />

private void addNewQuake(Quake _quake) {<br />

ContentResolver cr = getContentResolver();<br />

// Construct a where clause to make sure we don’t already have this<br />

// earthquake in the provider.<br />

String w = EarthquakeProvider.KEY_DATE + “ = “ +<br />

_quake.getDate().getTime();<br />

// If the earthquake is new, insert it into the provider.<br />

Cursor c = cr.query(EarthquakeProvider.CONTENT_URI,<br />

null, w, null, null);<br />

int dbCount = c.getCount();<br />

c.close();<br />

if (dbCount > 0) {<br />

ContentValues values = new ContentValues();<br />

values.put(EarthquakeProvider.KEY_DATE,<br />

_quake.getDate().getTime());<br />

values.put(EarthquakeProvider.KEY_DETAILS, _quake.getDetails());<br />

double lat = _quake.getLocation().getLatitude();<br />

double lng = _quake.getLocation().getLongitude();<br />

values.put(EarthquakeProvider.KEY_LOCATION_LAT, lat);<br />

values.put(EarthquakeProvider.KEY_LOCATION_LNG, lng);<br />

values.put(EarthquakeProvider.KEY_LINK, _quake.getLink());<br />

values.put(EarthquakeProvider.KEY_MAGNITUDE,<br />

_quake.getMagnitude());<br />

cr.insert(EarthquakeProvider.CONTENT_URI, values);<br />

203

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!