Creating a Data Class

In the project tool window, right-click the com.bignerdranch.android.criminalintent package and select NewKotlin File/Class to create a file named Crime.kt.

In Crime.kt, add fields to represent the crime’s ID, title, date, and status and a constructor that initializes the ID and date fields. In addition to the fields, add the data keyword to the class definition to make Crime a data class.

Listing 8.1  Adding the Crime data class (Crime.kt)

data class Crime(val id: UUID = UUID.randomUUID(),
                 var title: String = "",
                 var date: Date = Date(),
                 var isSolved: Boolean = false)

When importing Date, you will be presented with multiple options. Make sure to import java.util.Date.

UUID is a 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().

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

That is all you need for the Crime class and for CriminalIntent’s model layer in this chapter.

At this point, you have created the model layer and an activity that is capable of hosting a support fragment. Now you will get into the details of how the activity performs its duties as host.

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

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