Gutting CrimeLab

Now that you have a database, your next step is to change a lot of code inside of CrimeLab, swapping it to use mDatabase for storage instead of mCrimes.

Start out by doing some demolition. Strip out all the code related to mCrimes in CrimeLab.

Listing 14.7  Tearing down some walls (CrimeLab.java)

public class CrimeLab {
    private static CrimeLab sCrimeLab;

    private List<Crime> mCrimes;
    private Context mContext;
    private SQLiteDatabase mDatabase;

    public static CrimeLab get(Context context) {
        ...
    }

    private CrimeLab(Context context) {
        mContext = context.getApplicationContext();
        mDatabase = new CrimeBaseHelper(mContext)
                .getWritableDatabase();
        mCrimes = new ArrayList<>();
    }

    public void addCrime(Crime c) {
        mCrimes.add(c);
    }

    public List<Crime> getCrimes() {
        return mCrimes;
        return new ArrayList<>();
    }

    public Crime getCrime(UUID id) {
        for (Crime crime : mCrimes) {
            if (crime.getId().equals(id)) {
                return crime;
            }
        }
        return null;
    }
}

This will leave CriminalIntent in a state where it is not really working; you can see an empty list of crimes, but if you add a crime it will show an empty CrimePagerActivity. This is irritating, but fine for now.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.234.230