'Kotlin ListView not showing any text

I am currently trying to make a custom to-do list app. I have a ListView element set up and a text field with a buttons to add/remove elements from the list. However, when I add elements, no text shows up. I know that the fields add and remove correctly, the only problem is that there is no text. (image for reference)

Here is my activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:visibility="visible"
    tools:context=".MainActivity">

    ...

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:background="@color/white"
        android:choiceMode="multipleChoice"
        android:divider="@color/black"
        android:textColor="@color/black" />
</RelativeLayout>

And here is all the Kotlin code:

package com.example.to_do

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.SparseBooleanArray
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.ListView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val itemlist = arrayListOf<String>()
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, itemlist)

        val add = findViewById<Button>(R.id.add)
        val editText = findViewById<EditText>(R.id.editText)
        val listView = findViewById<ListView>(R.id.listView)
        val delete = findViewById<Button>(R.id.delete)

        listView.adapter = adapter

        add.setOnClickListener {
            val todoitem = editText.text.toString()
            if (todoitem.isEmpty() == false) {
                itemlist.add(editText.text.toString())
                adapter.notifyDataSetChanged()
            }
            editText.text.clear()
        }

        delete.setOnClickListener {
            val position: SparseBooleanArray = listView.checkedItemPositions
            val count = listView.count
            var item = count - 1
            while (item >= 0) {
                if (position.get(item)) {
                    adapter.remove(itemlist[item])
                }
                item--
            }

            position.clear()
            adapter.notifyDataSetChanged()
        }
    }
}

Can anybody help?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source