【问题标题】:How to keep track of my checked CheckBoxes in a Custom Adapter?如何在自定义适配器中跟踪我选中的复选框?
【发布时间】: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


    【解决方案1】:

    对象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
        }
    }
    

    【讨论】:

    • 等等,如果我只是按照我编辑过的原始帖子中的内容进行操作不是更好吗?我的意思是,我不明白为什么您需要两次反转旧状态。另外,当我取消选中我的复选框时呢?
    猜你喜欢
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多