androidkotlinandroid-checkbox

Android Kotlin how to check checkbox in multiple choice list view


I have a multiple choice list view. I wanting to check some of the checkboxes but not all of them. I am trying the code below but not sure where to go from there, or if that is even a start.

val listView = findViewById<ListView>(R.id.mylist_listview)
        val values = arrayOf("One", "Two", "Three")

        val list = ArrayList<String>()
        for (i in values.indices) {
            list.add(values[i])
        }
        val adapter = ArrayAdapter(this,
                android.R.layout.simple_list_item_multiple_choice, list)
        listView.setAdapter(adapter)


        val cntChoice = listView.getCount()

        for (i in 0 until cntChoice) {

            listView. //Something to check boxes 

            }

        }

Solution

  • To use multiple choice add listView.choiceMode = ListView.CHOICE_MODE_MULTIPLE and to check items you need to use listView.setItemChecked(position, true)