

For this example, we shall display item position and text with Toast. Now we shall implement ListView Item Click Listener to trigger execution of a specific code when an item is clicked. Implementing ListView Item Click Listener

If you observe, ListView is by default scrollable. Step 5: Set the adapter created in the previous step to the ListView. In the main code, to remove the item divider, we need to call the ListView divider and set it to null as shown below. Step 4: Initialize an Adapter (ArrayAdapter) with application context, resource to be used as View for each element of the list, and the array of elements itself as arguments. Step 3: Create a resource under android/res/layout that could be used for each element of the array while displaying in ListView.

var array = arrayOf("Melbourne", "Vienna", "Vancouver", "Toronto", "Calgary", "Adelaide", "Perth", "Auckland", "Helsinki", "Hamburg", "Munich", "New York", "Sydney", "Paris", "Cape Town", "Barcelona", "London", "Bangkok") Step 2: Have an array of elements, in the MainActivity.kt class file, to be displayed as ListView. Best example of it is our devices Contact List. Step 1: Create ListView in activity_main.xml layout file. ListView is used when you have to show items in a vertically scrolling list. Also, we are calling notifyDataSetChanged() to reload the data in the recyclerview.Steps to display items of an array as Kotlin Android ListView ListView is implemented by importing class. In deleteItem, we are removing the item from listData at position index. Android ListView is a view which contains the group of items and displays in a scrollable list.In this tutorial we are creating a listview with multiple items and now we are deleting the selected long press list element using setOnItemLongClickListener() method. Inside the bind method, we are adding one click listener to the delete button. How to delete only clicked item from listview while user long press that list element.Inside the adapter class, we will get the reference of the delete button and on click of this button, we will call one function that deletes the data:Ĭlass MyAdapter ( private val data : List ) : RecyclerView.
