Android Programming The Big Nerd Ranch Guide, 3rd Edition

(Brent) #1

Chapter 7  UI Fragments and the Fragment Manager


134

Creating the Crime class


In the project tool window, right-click the com.bignerdranch.android.criminalintent package and
select New → Java Class. Name the class Crime and click OK.


In Crime.java, add fields to represent the crime’s ID, title, date, and status and a constructor that
initializes the ID and date fields (Listing 7.3).


Listing 7.3  Adding to Crime class (Crime.java)


public class Crime {


private UUID mId;
private String mTitle;
private Date mDate;
private boolean mSolved;


public Crime() {
mId = UUID.randomUUID();
mDate = new Date();
}
}


UUID is a Java utility class included in the Android framework. It provides an easy way to generate
universally unique ID values. In the constructor you generate a random unique ID by calling
UUID.randomUUID().


Android Studio may find two classes with the name Date. Use the Option+Return (or Alt+Enter)
shortcut to manually import the class. When asked which version of the Date class to import, choose
the java.util.Date version.


Initializing the Date variable using the default Date constructor sets mDate to the current date. This will
be the default date for a crime.

Free download pdf