Kotlin ListView with ViewHolder

(Last Updated On: August 17, 2018)

In this post we will create a ListView with ViewHolder adapter but we will write the code in Kotlin.

For creating xml files for ListView and list items, follow the steps from this post.

Next, you have to create a new Kotlin class for the adapter. Name the new created file MyCustomAdapter

MyCustomAdapter.kt

What’s different in this adapter written in Kotlin compared to the one in Java is that we had to create rowView variable in order to be able to inflate the list_item layout to the view. In Kotlin if we try to inflate the layout directly to the view: View? attribute we will get an error “Val cannot be reassigned”.

MainActivity.kt