Converting to model objects
The last important thing to do is to call close() on your Cursor. This bit of housekeeping is important.
If you do not do it, your Android device will spit out nasty error logs to berate you. Even worse, if you
make a habit out of it, you will eventually run out of open file handles and crash your app. So: Close
your cursors.
CrimeLab.getCrime(UUID) will look similar to getCrimes(), except it will only need to pull the first
item, if it is there.
Listing 14.19 Rewriting getCrime(UUID) (CrimeLab.java)
public Crime getCrime(UUID id) {
return null;
CrimeCursorWrapper cursor = queryCrimes(
CrimeTable.Cols.UUID + " = ?",
new String[] { id.toString() }
);
try {
if (cursor.getCount() == 0) {
return null;
}
cursor.moveToFirst();
return cursor.getCrime();
} finally {
cursor.close();
}
}
That completes a few moving pieces:
- You can insert crimes, so the code that adds Crime to CrimeLab when you press the New Crime
action item now works. - You can successfully query the database, so CrimePagerActivity can see all the Crimes in
CrimeLab, too. - CrimeLab.getCrime(UUID) works, too, so each CrimeFragment displayed in
CrimePagerActivity is showing the real Crime.
Now you should be able to press New Crime and see the new Crime displayed in CrimePagerActivity.
Run CriminalIntent and verify that you can do this. If you cannot, double-check your implementations
from this chapter so far.