Wednesday, March 11, 2020

android kotlin - ListView delete item on click

MainActivity.kt

package com.cfsuman.kotlintutorials

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


class MainActivity : Activity() {
    lateinit var context:MainActivity

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

        // get the context
        context = this

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


        // list to populate list view
        val list = mutableListOf(
            "Golden yellow",
            "Han purple",
            "Hansa yellow",
            "Jazzberry jam"
        )

        // initialize an array adapter
        val adapter:ArrayAdapter<String> = ArrayAdapter<String>(
            context,
            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 ->

            // remove clicked item from list view
            list.removeAt(position)
            adapter.notifyDataSetChanged()
        }



        // another list to populate second list view
        val list2 = mutableListOf(
            "Android green",
            "Antique brass",
            "Antique white",
            "Avocado"
        )


        // initialize another array adapter
        val adapter2:ArrayAdapter<String> = ArrayAdapter<String>(
            context,
            android.R.layout.simple_dropdown_item_1line,
            list2
        )


        // attach the array adapter with second list view
        listView2.adapter = adapter2


        // second list view item click listener
        listView2.onItemClickListener = AdapterView.OnItemClickListener {
                parent, view, position, id ->

            val clickedItem = parent.getItemAtPosition(position).toString()

            // show an alert dialog to confirm
            AlertDialog.Builder(context).apply {
                setTitle("Item: $clickedItem")
                setMessage("Are you want to delete it from list view?")

                setPositiveButton("Delete"){_,_->
                    // remove clicked item from second list view
                    list2.removeAt(position)
                    adapter2.notifyDataSetChanged()
                }

                setNeutralButton("Cancel"){_,_->}
            }.create().show()
        }
    }
}
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">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:choiceMode="singleChoice"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/listView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:choiceMode="singleChoice"
        android:background="#F8F8F8"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/listView" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android examples