Following up on part 2 which demonstrates how to create your Android app and configure Kotlin, we’ll begin building the heart and soul of a Todo List application - the list!
Let’s begin by defining our model. We’re going to create a simple class that has two fields, one for description and one for whether or not it’s completed. Here is how this class will look in Kotlin:
data class Task(var description: String, var completed: Boolean = false) : Serializable
You may not believe it, but that’s all we need. Let’s talk about what’s happening here:
- A data class is a special class in Kotlin that provides you with default behaviors for all your Object methods like
copy(). Read more here.
- Kotlin allows for default constructors to be defined right with the class name.
- Kotlin allows for default parameters. So in this case, we have a constructor that can be used as
Task("Description")and it will default to incomplete, or we can call it with
Task("Description", true)to set the initial value of the completed boolean.
- We’ve had our class implement Serializable. In this simple app, we’re just going to save the data to a text file instead of over complicating it with SQLite.