Creating a Data Class

In the project tool window, right-click the 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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.