12. Key Kotlin Changes and Explanations

Key Kotlin Changes and Explanations:

  • lateinit: Used for properties that will be initialized later (in onCreate). This avoids null checks.
  • val vs. var: val for read-only variables, var for mutable variables.
  • Kotlin Collections: ArrayList<String>() is how you create an ArrayList in Kotlin. Kotlin also has other collection types like List, MutableList, Map, etc.
  • Concise Click Listener: buttonAdd.setOnClickListener { ... } is a more compact way to set a click listener.
  • String Checks: newItem.isNotEmpty() is a cleaner way to check if a string is not empty.
  • Clearing EditText: editTextItem.text.clear() is the Kotlin way to clear the text.
  • List Access: todoItems[position] is how you access elements in a list.
  • Null Safety: Kotlin has a strong type system that helps prevent null pointer exceptions. lateinit and other features are designed to work with null safety in mind.

activity_main.xml (Layout): This remains the same as the Java version.

Steps:

  1. Create a new Android Studio project and choose Kotlin as the language.
  2. Replace the contents of MainActivity.kt and create TodoAdapter.kt with the code provided above.
  3. Make sure your activity_main.xml layout file is correct.
  4. Run the app.


 Swift for iOS

Swift

import UIKit

 

class ViewController: UIViewController {

 

    @IBOutlet weak var editTextItem: UITextField! // Outlet for the text input

    @IBOutlet weak var buttonAdd: UIButton! // Outlet for the add button

    @IBOutlet weak var tableViewItems: UITableView! // Outlet for the table view

 

    var todoItems: [String] = [] // Array to store to-do items

 

    override func viewDidLoad() {

        super.viewDidLoad()

 

        tableViewItems.dataSource = self // Set the data source for the table view

        tableViewItems.delegate = self // Set the delegate for the table view

 

        buttonAdd.addTarget(self, action: #selector(addItem), for: .touchUpInside) // Add target for button click

    }

 

    @objc func addItem() { // Function to handle adding an item

        guard let newItem = editTextItem.text?.trim(), !newItem.isEmpty else { // Check for empty input

            return // Exit if input is empty

        }

 

        todoItems.append(newItem) // Add the item to the array

        tableViewItems.reloadData() // Reload the table view to display the new item

        editTextItem.text = "" // Clear the text field

    }

}

 

// Conform to UITableViewDataSource and UITableViewDelegate

extension ViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return todoItems.count // Return the number of items

    }

 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) // Dequeue a reusable cell

        cell.textLabel?.text = todoItems[indexPath.row] // Set the cell text

        return cell

    }

}