Adding a Suspect to the Model Layer

Next, open Crime.java and add a new member variable to give Crime a field that will hold the name of a suspect.

Listing 15.2  Adding suspect field (Crime.java)

public class Crime {
    ...
    private boolean mSolved;
    private String mSuspect;

    public Crime() {
        this(UUID.randomUUID());
    }
    ...
    public void setSolved(boolean solved) {
        mSolved = solved;
    }

    public String getSuspect() {
        return mSuspect;
    }

    public void setSuspect(String suspect) {
        mSuspect = suspect;
    }
}

Now you need to add an additional field to your crime database. First, add a suspect column to CrimeDbSchema.

Listing 15.3  Adding suspect column (CrimeDbSchema.java)

public class CrimeDbSchema {
    public static final class CrimeTable {
        public static final String NAME = "crimes";

        public static final class Cols {
            public static final String UUID = "uuid";
            public static final String TITLE = "title";
            public static final String DATE = "date";
            public static final String SOLVED = "solved";
            public static final String SUSPECT = "suspect";
        }
    }
}

Add the column in CrimeBaseHelper, also. (Notice that the new code begins with a quotation mark and comma after CrimeTable.Cols.SOLVED +.)

Listing 15.4  Adding suspect column again (CrimeBaseHelper.java)

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + CrimeTable.NAME + "(" +
            " _id integer primary key autoincrement, " +
            CrimeTable.Cols.UUID + ", " +
            CrimeTable.Cols.TITLE + ", " +
            CrimeTable.Cols.DATE + ", " +
            CrimeTable.Cols.SOLVED + ", " +
            CrimeTable.Cols.SUSPECT +
            ")"
    );
}

Next, write to the new column in CrimeLab.getContentValues(Crime).

Listing 15.5  Writing to suspect column (CrimeLab.java)

    private static ContentValues getContentValues(Crime crime) {
        ContentValues values = new ContentValues();
        values.put(CrimeTable.Cols.UUID, crime.getId().toString());
        values.put(CrimeTable.Cols.TITLE, crime.getTitle());
        values.put(CrimeTable.Cols.DATE, crime.getDate().getTime());
        values.put(CrimeTable.Cols.SOLVED, crime.isSolved() ? 1 : 0);
        values.put(CrimeTable.Cols.SUSPECT, crime.getSuspect());

        return values;
    }
}

Now read from it in CrimeCursorWrapper.

Listing 15.6  Reading from suspect column (CrimeCursorWrapper.java)

    public Crime getCrime() {
        String uuidString = getString(getColumnIndex(CrimeTable.Cols.UUID));
        String title = getString(getColumnIndex(CrimeTable.Cols.TITLE));
        long date = getLong(getColumnIndex(CrimeTable.Cols.DATE));
        int isSolved = getInt(getColumnIndex(CrimeTable.Cols.SOLVED));
        String suspect = getString(getColumnIndex(CrimeTable.Cols.SUSPECT));

        Crime crime = new Crime(UUID.fromString(uuidString));
        crime.setTitle(title);
        crime.setDate(new Date(date));
        crime.setSolved(isSolved != 0);
        crime.setSuspect(suspect);

        return crime;
    }
}

If CriminalIntent is already installed on your device, your existing database will not have the suspect column, and your new onCreate(SQLiteDatabase) will not be run to add the new column, either. As we said earlier, the easiest solution is to wipe out your old database in favor of a new one. (This happens a lot in app development.)

First, uninstall the CriminalIntent app by opening the app launcher screen and dragging the CriminalIntent icon to the top of the screen. All your sandbox storage will get blown away, along with the out-of-date database schema, as part of the uninstall process. Next, run CriminalIntent from Android Studio. A new database will be created with the new column as part of the app installation process.

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

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