【发布时间】:2019-10-10 18:26:16
【问题描述】:
我正在尝试找到一种方法来跟踪 CheckBoxes 中 ListView 中的所有选中项目。
我目前正在创建我的自定义适配器来处理所有事情,但能够跟踪我所有选中的项目,以便我以后可以通过按下按钮删除选中的项目。
我不确定如何处理它。我花了几个小时在谷歌上搜索如何做到这一点,但其中大部分都使用 Java,我不确定如何正确地将其用法转换为 Kotlin,或者即使它适用于我的 android 应用程序。如果有人可以帮助我,将不胜感激
DSArrayAdapter.kt - 我的自定义阵列适配器:
class DSArrayAdapter(context: Context, resource: Int, list: ArrayList<Contacts>) : ArrayAdapter<Contacts>(context, resource, list) {
private val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
override fun getView(position: Int, convertView: View?, parent: ViewGroup) : View {
val rowView = inflater.inflate(R.layout.activity_listview, null)
val item_name = rowView.findViewById<TextView>(R.id.contact_name)
val item_checkbox = rowView.findViewById<CheckBox>(R.id.checked)
item_name.setText(getItem(position)?.cname.toString())
item_checkbox.setOnClickListener(View.OnClickListener{
val contact = getItem(position) as Contacts
contact.cchecked = !contact.cchecked
item_checkbox.isChecked = contact.cchecked
})
})
return rowView
}
}
Contacts.kt - 我的类来保存我的联系人条目的属性:
class Contacts(val cname: String, val cphone: Int, val cchecked: Boolean) {
}
【问题讨论】:
标签: android kotlin custom-adapter android-checkbox