Simple ListView using ArrayAdapter

A simple ListView in a layout file.

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>

Initialize the ListView with a list of strings, and using a system provided list item layout and ArrayAdapter.

val strings = arrayOf("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")

val adapter = ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, strings)
list_view.setAdapter(adapter)

list_view.setOnItemClickListener({ parent, view, position, id ->
    val str = list_view.getItemAtPosition(position) as String
    Toast.makeText(view.context, str, Toast.LENGTH_SHORT).show()
})

Search within Codexpedia

Custom Search

Search the entire web

Custom Search