Object Is Not Getting Removed From List Oncheckedchange Listener Of Check Box
I have a list of contacts. I have a check box to select the contacts. These selected contacts I want to add in another list called invitation array list. I have created a method to
Solution 1:
OK, so there are few silly mistakes I say,
a. Replace
finalContactcontact= contactArrayList.get(position);
with
finalContactcontact= contactArrayList.get(holder.getAdapterPosition());
b. You are adding Object of Class Invitation
Invitation invitation = new Invitation();
invitation.setSender_id(mUserId);
invitation.setDate(date);
invitation.setInvitee_no(contact.getmMobileNo());
invitation.setStatus("0");
invitation.setUser_name(contact.getmUserName());
invitationArrayList.add(invitation);
c. You are trying to remove object of Class Contacts
else {
invitationArrayList.remove(contact);
Log.e("inviteList",String.valueOf(invitationArrayList.size()));
}
Solution
in onBindViewHolder
holder.checkBox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
@OverridepublicvoidonCheckedChanged(CompoundButton compoundButton, boolean b) {
contactArrayList.get(holder.getAdapterPosition()).setSelected(b);
Log.e("inviteList",String.valueOf(invitationArrayList.size()));
}
});
then make a method like
private void updateInvites(){
invitationArrayList.clear();
for(Contacts contacts : contactsArrayList){
if(contacts.isSelected()){
invite(contact);
}
}
}
here
contact.setSeletected(boolean status)
andcontact.isSelected()
are setters and getters respectively
Solution 2:
instead of using object to remove use index to remove item:
invitationArrayList.remove(position);
it will work.
You are getting index out of bound because there may be possibility user is removing the item before adding it. may be particular element in not present in your invitationArrayList. So for this try this code:
if (invitationArrayList!=null && invitationArrayList.size() > 0 && invitationArrayList.size() > position +1){
invitationArrayList.remove(position);
}
Post a Comment for "Object Is Not Getting Removed From List Oncheckedchange Listener Of Check Box"