I'm trying to delete Contacts from my list of Contacts according to the checked boxes next to the contact names once I've pressed the delete button. Now, I was able to get the code to work for when I had one contact within the list but, once I add in any more than 1, it doesn't seem to work, regardless of how I adjust my loop. Either it crashes, which I suspect has to do with an out of bounds issue within the list, or, with my current code, doesn't do anything at all. I've searched around for some solutions but none of them have worked so far. If anyone could help me, that'd be appreciated. I've included the corresponding code below with my current implementation:
MainActivity.kt:
class MainActivity : AppCompatActivity() {
var CONTACT_DETAIL_ACTIVITY_REQUEST = 0
var listContacts = ArrayList<Contacts>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setTitle("Contacts")
val contactAdapter = DSArrayAdapter(this, R.layout.activity_listview, listContacts)
val listView1 = findViewById<ListView>(R.id.contact_list)
listView1.setAdapter(contactAdapter)
listContacts.add(Contacts("Faisal is spoiled", 732, false))
listContacts.add(Contacts("Dawg", 666, false))
contactAdapter.notifyDataSetChanged()
val add_new_contact = findViewById<Button>(R.id.add_contacts)
val delete_contacts = findViewById<Button>(R.id.delete_contacts)
//*** Delete Function ***///
delete_contacts.setOnClickListener({
var newlistContacts = ArrayList<Contacts>()
for (x in listContacts) {
if (x.cchecked == false) {
newlistContacts.add(x)
}
}
listContacts = newlistContacts
contactAdapter.notifyDataSetChanged()
})
//*** Delete Function ***///
add_new_contact.setOnClickListener {
val name = ""
val phone_number = 1
val relationship = ArrayList<String>()
val intent1 = Intent(this,ContactDetails::class.java)
startActivityForResult(intent1, CONTACT_DETAIL_ACTIVITY_REQUEST)
}
}
}
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
}
}
You are changing your listContacts with
listContacts = newListContacts
But your adapter does not know newListContacts. You could copy call listContacts.clear() and listContacts.addAll(newListContacts) But it will be a lot of work, if you copy lists.
Try to change your delete block to:
delete_contacts.setOnClickListener({
listContacts.removeIf { it.cchecked }
contactAdapter.notifyDataSetChanged()
})