ListView
CheckBox에서 내 모든 체크 항목을 추적하는 방법을 찾으려고합니다 .
현재 모든 것을 처리하기 위해 사용자 지정 어댑터를 만들고 있지만 나중에 버튼을 눌러 확인 된 항목을 삭제할 수 있도록 확인 된 모든 항목을 추적 할 수 있습니다.
어떻게 처리해야할지 모르겠습니다. 나는 그것을 수행하는 방법에 대해 인터넷 검색에 몇 시간을 보냈지 만 대부분은 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) {
}
개체 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
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다