From the course: Android Development Essential Training: Manage Data with Kotlin

Model an entity in a Kotlin data class

From the course: Android Development Essential Training: Manage Data with Kotlin

Start my 1-month free trial

Model an entity in a Kotlin data class

- [Narrator] Regardless of where your app gets its data, a web service, a text file, or a local database, you can manage that data in device memory using a class sometimes known as an entity class. In Kotlin, you can set this up as a data class that generates really useful components, such as a pre-built to string function, and automatic getters and setters. I'll start by creating a new package under my base package. I'll name the new sub-package Data. And I'll create a Kotlin Class there. I'll select New Kotlin File or Class, I'll name the file Monster, and I'll set the kind as Class. And click OK. Now to make this into a data class, just add the keyword data right before the class declaration. If your class is very simple and only has properties, you don't need a code block, so I'll get rid of the braces and instead put in a pair of parentheses. This will serve as both the class declaration and as its primary constructor. Each property of the class gets declared with either val or var. Val means it's an immutable value, it can't be changed. And var means it can be changed, it's mutable. I'll set the names of my properties to match the names used in the JSON file. I'll start with monsterName and I'll set its type as a string. Then I'll set imageFile and I'll set its type as a string as well and a caption and these are all strings so far. And then description. Next I need a couple of numbers, the price will be a double value and scariness will be an integer. And again these types match what I expect in the JSON file where the price is a fractional value and scariness is always a whole number. That's my complete data class. I'll go to my ui.mainpackage and open main fragment and I'll add some code here in the oncreate view function right before I inflate my layout file. I'll create a new variable called monster and I'll initialize it by calling that monster class is constructor. When I select the monster class it's imported at the top of the file and now I'll be prompted for all the properties I'll set the monster's name as Bob, then I'll set the name of the file as my file. And it doesn't matter right now what these values are, I'll set a caption and a description. Then I'll add in a double value as a price, so I'll put in .19 and a scariness value of three. Now I'll output that in logcat with log.i I'll pass in a tag of monster logging and then I'll pass in the monster objects to string function. I'll run the application on my device. I'll go to logcat and filter on monster logging my tag and there's the result. I've successfully created an instance of my data class and output its contents into the logcat window. So now I have a class that I can use to model my data regardless of where it comes from.

Contents