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>
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHBsaQGi_zVsbjHnYFJHOQoRE93M5j8a5pDj0tnBKpWte27f9Z-iRErQYnyNsV8KKcKdViK9ZxDpGXO5xMhd8pn9ylc1BR8uFEU_r7YsBIg-7qEmIBW-fGL1trJCFGiVoA6qVkmAuUfDF-xsUHyGX2GS_V9zwiP6Xk5tsmaxVPEmUY_PG1jk6NJeSCwA/s1600/android_kotlin_listview_delete_item_on_click.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCyVnbU67Uf1Nd0ZV9_gReKB_Z5gwV-_xp6T650tiKy7d_pGCqPMgObinj-xVguSo4ObaiCadzK4Y2fxoHzQNQF-_YQuLf38277g7Jq-B5dxxYqYvs4kfPVpbcXuinrj2mxNprJ2zlE4t-v8k6astTu7bJRrD2q0m8MGWlkcVV4ZtLSOs8DkxI7V5DQg/s1600/android_kotlin_listview_delete_item_on_click2.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8EcXyTc6ZgEvrGHChtPyOzEA4kxwtcQBP0TUqonRa0QpT_L4LSvprJg_NP_6_cZ6102O7UZb6G764AVXdm1-tEDM5DzheUtYfUWU7w66HAuxcatmpht38xGKNqHdls0eEIQMrxErvyQfI6DOEdYkVCn_bKA0fVqrSwjYD-7L5twWa6WyA3X69bl3rNA/s1600/android_kotlin_listview_delete_item_on_click3.png)