如何在自定义适配器中跟踪我选中的复选框?
How to keep track of my checked CheckBoxes in a Custom Adapter?
我正在尝试找到一种方法来跟踪 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 用于保存我的联系人条目的属性:
class Contacts(val cname: String, val cphone: Int, val cchecked: Boolean) {
}
对象 Contacts
似乎已经有一个字段来跟踪该项目是否被选中。所以,你可以这样使用:
首先,制作cchecked
变量。这样就可以改了。
// cchecked must be var so you can change between checked/unchecked
class Contacts(val cname: String, val cphone: Int, var cchecked: Boolean) {
}
然后,在适配器中:
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)
val contact = getItem(position) as Contacts
// Set text
item_name.setText(contact.cname)
// Set checkbox state
item_checkbox.isChecked = contact.cchecked
// If does not have a click listener yet, set one.
// View will be re-used. So, you don't need to set a listener everytime
if(!item_checkbox.hasOnClickListeners()) {
item_checkbox.setOnClickListener {
// Get the old state
val contact = getItem(position) as Contacts
// Invert the old state in the contact
contact.cchecked = !contact.cchecked
// Apply the new state to the checkbox
item_checkbox.isChecked = contact.cchecked
}
}
return rowView
}
}
我正在尝试找到一种方法来跟踪 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 用于保存我的联系人条目的属性:
class Contacts(val cname: String, val cphone: Int, val cchecked: Boolean) {
}
对象 Contacts
似乎已经有一个字段来跟踪该项目是否被选中。所以,你可以这样使用:
首先,制作cchecked
变量。这样就可以改了。
// cchecked must be var so you can change between checked/unchecked
class Contacts(val cname: String, val cphone: Int, var cchecked: Boolean) {
}
然后,在适配器中:
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)
val contact = getItem(position) as Contacts
// Set text
item_name.setText(contact.cname)
// Set checkbox state
item_checkbox.isChecked = contact.cchecked
// If does not have a click listener yet, set one.
// View will be re-used. So, you don't need to set a listener everytime
if(!item_checkbox.hasOnClickListeners()) {
item_checkbox.setOnClickListener {
// Get the old state
val contact = getItem(position) as Contacts
// Invert the old state in the contact
contact.cchecked = !contact.cchecked
// Apply the new state to the checkbox
item_checkbox.isChecked = contact.cchecked
}
}
return rowView
}
}