Tuesday, March 10, 2020

android kotlin - ListView remove item programmatically

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.os.Bundle
import android.widget.*


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

        // get the widgets reference from XML layout
        val button = findViewById<Button>(R.id.button)
        val listView = findViewById<ListView>(R.id.listView)
        val textView = findViewById<TextView>(R.id.textView)


        // list to populate list view
        val list = mutableListOf(
            "Black bean",
            "Black coffee",
            "Black chocolate",
            "French lilac",
            "Bronze",
            "Dark sienna",
            "Deep taupe",
            "Fern green"
        )


        // show the list size
        textView.text = "List elements : ${list.size}"


        // initialize an array adapter
        val adapter:ArrayAdapter<String> = ArrayAdapter(
            this,
            android.R.layout.simple_dropdown_item_1line,list
        )


        // attach the array adapter with list view
        listView.adapter = adapter


        // list view item click listener
        listView.onItemClickListener = AdapterView.OnItemClickListener {
                parent, view, position, id ->
            val selectedItem = parent.getItemAtPosition(position)
            textView.text = "Selected : $selectedItem"
        }



        // remove list view items programmatically
        button.setOnClickListener {
            // disable the button itself
            it.isEnabled = false

            // remove list view item by element name
            list.remove("Black coffee")

            // remove item by index
            list.removeAt(3)

            // remove all elements which name ends with "c"
            list.removeAll {
                it.endsWith("c",false)
            }

            // remove all elements that match the collection
            val elementsToRemove = listOf("Cameo pink","Dark sienna")
            list.removeAll(elementsToRemove)

            // remove if element starts with "f", ignore case
            list.removeIf {
                it.startsWith("f",ignoreCase = true)
            }

            // show the list size, after removing elements
            textView.text = "List elements : ${list.size}"

            // finally, notify the adapter for data set changed
            adapter.notifyDataSetChanged()
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootLayout"
    android:background="#DCDCDC"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Remove Items"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="24dp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:gravity="center"
        android:textColor="#333399"
        android:textSize="24sp"
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>