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.

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

204<br />

}<br />

}<br />

earthquakes.add(_quake);<br />

addQuakeToArray(_quake);<br />

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

if (_quake.getMagnitude() > minimumMagnitude) {<br />

// Add the new quake to our list of earthquakes.<br />

earthquakes.add(_quake);<br />

}<br />

}<br />

// Notify the array adapter of a change.<br />

aa.notifyDataSetChanged();<br />

2. Create a new loadQuakesFromProvider method that loads all the earthquakes from the Earthquake<br />

Provider and inserts them into the array list using the addQuakeToArray method created<br />

in Step 1.<br />

private void loadQuakesFromProvider() {<br />

// Clear the existing earthquake array<br />

earthquakes.clear();<br />

ContentResolver cr = getContentResolver();<br />

// Return all the saved earthquakes<br />

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

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

if (c.moveToFirst())<br />

{<br />

do {<br />

// Extract the quake details.<br />

Long datems = c.getLong(EarthquakeProvider.DATE_COLUMN);<br />

String details;<br />

details = c.getString(EarthquakeProvider.DETAILS_COLUMN);<br />

Float lat = c.getFloat(EarthquakeProvider.LATITUDE_COLUMN);<br />

Float lng = c.getFloat(EarthquakeProvider.LONGITUDE_COLUMN);<br />

Double mag = c.getDouble(EarthquakeProvider.MAGNITUDE_COLUMN);<br />

String link = c.getString(EarthquakeProvider.LINK_COLUMN);<br />

Location location = new Location(“dummy”);<br />

location.setLongitude(lng);<br />

location.setLatitude(lat);<br />

Date date = new Date(datems);<br />

Quake q = new Quake(date, details, location, mag, link);<br />

addQuakeToArray(q);<br />

} while(c.moveToNext());<br />

}<br />

c.close();<br />

}

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

Saved successfully!

Ooh no, something went wrong!