Skip to content Skip to sidebar Skip to footer

Kotlin Setonclicklistener For A Listview

I have the following code: class BookListActivity : AppCompatActivity() { var array = arrayOf('Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6', 'Item 7', 'Item 8', '

Solution 1:

You have to handle all the parameters for AdapterView.OnItemClickListener

Your listener should look like this:

listView.setOnItemClickListener { parent, view, position, id -> 
    val element = adapter.getItemAtPosition(position) // The item that was clickedval intent = Intent(this, BookDetailActivity::class.java)
    startActivity(intent)
}

Solution 2:

Try this solution for listview item click listener.

listView.setOnItemClickListener = AdapterView.OnItemClickListener {parent,view, position, id ->
          // Get the selected item text from ListViewval selectedItem = parent.getItemAtPosition(position) as String

          val intent = Intent(this, BookDetailActivity::class.java)
          startActivity(intent)
    }

Post a Comment for "Kotlin Setonclicklistener For A Listview"