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 ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.