Reputation: 2663
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
}
}
Upvotes: 1
Views: 3680
Reputation: 162
Kotlin: Multiple choice checkbox checked & add in list And Unchecked checkbox then remove in same list of element
I hope your checkbox in static form: like you knowing fixed checkbox on my UI then this code used
add code in your class file
private var socialList: String? = null
private var socialMArray = ArrayList<String>()
private var chkArraySocial = arrayOfNulls<CheckBox>(7)
private var strFaceBook: String? = null
private var strTwitter: String? = null
private var strInstagram: String? = null
private var strWhatsApp: String? = null
private var strSnapChat: String? = null
private var strLinkedIn: String? = null
private var strOtherSocial: String? = null
private var regex = "\\[|\\]"
/** social media list **/
strTwitter = getResStringLanguage(R.string.str_twitter, "en")
strInstagram = getResStringLanguage(R.string.str_instagram, "en")
strFaceBook = getResStringLanguage(R.string.str_facebook, "en")
strWhatsApp = getResStringLanguage(R.string.str_whatsapp, "en")
strLinkedIn = getResStringLanguage(R.string.str_linkedin, "en")
strSnapChat = getResStringLanguage(R.string.str_snapchat, "en")
strOtherSocial = getResStringLanguage(R.string.str_other, "en")
chkArraySocial[0] = binding.cbFbSocial
chkArraySocial[0]?.text = strFaceBook
chkArraySocial[0]?.setOnCheckedChangeListener(clickListSocial)
chkArraySocial[1] = binding.cbTwitterSocial
chkArraySocial[1]?.text = strTwitter
chkArraySocial[1]?.setOnCheckedChangeListener(clickListSocial)
chkArraySocial[2] = binding.cbInstagramSocial
chkArraySocial[2]?.text = strInstagram
chkArraySocial[2]?.setOnCheckedChangeListener(clickListSocial)
chkArraySocial[3] = binding.cbWhatsappSocial
chkArraySocial[3]?.text = strWhatsApp
chkArraySocial[3]?.setOnCheckedChangeListener(clickListSocial)
chkArraySocial[4] = binding.cbLinkedinSocial
chkArraySocial[4]?.text = strLinkedIn
chkArraySocial[4]?.setOnCheckedChangeListener(clickListSocial)
chkArraySocial[5] = binding.cbSnapchatSocial
chkArraySocial[5]?.text = strSnapChat
chkArraySocial[5]?.setOnCheckedChangeListener(clickListSocial)
chkArraySocial[6] = binding.cbOtherSocial
chkArraySocial[6]?.text = strOtherSocial
chkArraySocial[6]?.setOnCheckedChangeListener(clickListSocial)
private val clickListSocial: CompoundButton.OnCheckedChangeListener = CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
val checkedId: Int = buttonView?.id!!
for (i in chkArraySocial.indices) {
val current = chkArraySocial[i]
if (current?.id == checkedId) {
val checkBoxSocial = view?.findViewById<CheckBox>(current.id)
val cbValueSocial = checkBoxSocial?.text.toString()
if (isChecked){
socialMArray.add(cbValueSocial)
val formattedDirections = socialMArray.toString().trim().replace("\\.\\s?".toRegex(), "\\.\n")
socialList = formattedDirections.replace(regex.toRegex(),"")
Log.i("TAG", "social media add list: $socialList")
}else{
socialMArray.remove(cbValueSocial)
val formattedDirections = socialMArray.toString().trim().replace("\\.\\s?".toRegex(), "\\.\n")
socialList = formattedDirections.replace(regex.toRegex(),"")
Log.i("TAG", "social media remove list: $socialList")
}
}
}
}
language change on select checkbox value
fun getResStringLanguage(id: Int, lang: String?): String {
val res = resources
val conf = res.configuration
val savedLocale = conf?.locale
val confAr = resources.configuration
confAr?.locale = Locale(lang!!)
val metrics = DisplayMetrics()
val resources = Resources(resources.assets, metrics, confAr)
val string = resources.getString(id)
conf?.locale = savedLocale
res.updateConfiguration(conf, null)
return string
}
Upvotes: 0
Reputation: 2663
I found the solution.
for (i in 0 until cntChoice) {
listView.setItemChecked(i,true)
}
Upvotes: 0
Reputation: 6275
To use multiple choice add listView.choiceMode = ListView.CHOICE_MODE_MULTIPLE
and to check items you need to use listView.setItemChecked(position, true)
Upvotes: 2