Sling Academy
Home/Kotlin/Building a To-Do List App with Kotlin Collections

Building a To-Do List App with Kotlin Collections

Last updated: November 30, 2024

Creating a To-Do List App with Kotlin Collections

In this article, we will build a simple to-do list application using Kotlin collections. A to-do list app allows you to manage and organize tasks efficiently, and using Kotlin collections will enable us to leverage features like lists, sets, and maps effectively.

Getting Started

First, let's create a new Kotlin project. If you are using IntelliJ IDEA, simply create a new Kotlin/Java project. Name the project 'ToDoListApp'.

Main Layout

Create a simple user interface that consists of a text field to enter tasks and a button to add tasks to the list.


val taskList = mutableListOf<String>()

fun addTask(task: String) {
    if (task.isNotBlank()) {
        taskList.add(task)
        println("Task added: $task")
    } else {
        println("Invalid task entry.")
    }
}

fun displayTasks() {
    println("Your Tasks:")
    taskList.forEachIndexed { index, task ->
        println("${index + 1}. $task")
    }
}

Core Functions

We can organize our application logic into a few core functions:

  • addTask: Adds a new task to the list if it's not blank.
  • displayTasks: Displays all the tasks in the list with their index.

Adding More Features

To make the application more functional, let's add features like removing a task, marking a task as completed, and prioritizing tasks.


fun removeTask(index: Int) {
    if (index in 1..taskList.size) {
        taskList.removeAt(index - 1)
        println("Task removed at position: $index")
    } else {
        println("Invalid index.")
    }
}

fun markTaskAsCompleted(index: Int) {
    if (index in 1..taskList.size) {
        val task = taskList[index - 1]
        println("Task $index: '$task' marked as completed.")
    } else {
        println("Invalid index.")
    }
}

With these additional functions, we can enhance our to-do list by removing or marking tasks:

  • removeTask: Removes the task at the specified position.
  • markTaskAsCompleted: A placeholder to denote a task's completion. In a full application, this could update the task's status.

Conclusion

We've set up a basic to-do list app using Kotlin collections. This simple console application illustrates how Kotlin lists are used to manage a dynamic list of tasks. As you advance, consider expanding this by integrating a UI using Android's framework or Kotlin's coroutines for concurrent task management.

Next Article: Using Collections for Data Processing in APIs in Kotlin

Previous Article: Joining Collections into Strings with `joinToString` in Kotlin

Series: Kotlin Collections

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin