mardi 2 avril 2019

How to save checkBox value through onCheckedChangeListener() in android

I have used checkbox in recycler view getting data from phone's contact. Each recycler item has check box. And if the check box is checked that item is saved in SQLite database and on unchecked, it is removed from the database. Here is the onCheckedChangeListener() function.

@Override
public void onCheckedChangeListener(String phoneNO ,boolean isCheck) {
    if (isCheck){
        if(phoneNO.substring(0,3).equals("+92"))
        {
            phoneNO = "0"+phoneNO.substring(3);
            databaseHelper.addIgnoredPhoneNumber(phoneNO.replace(" ", ""));
        }else{
            Toast.makeText(this, ""+phoneNO, Toast.LENGTH_SHORT).show();
            databaseHelper.addIgnoredPhoneNumber(phoneNO.replace(" ", ""));
        }
    }else{
        if(phoneNO.substring(0,3).equals("+92"))
        {
            phoneNO = "0"+phoneNO.substring(3);
            databaseHelper.removeContact(phoneNO.replace(" ", ""));
        }else {
            Toast.makeText(this, ""+phoneNO, Toast.LENGTH_SHORT).show();
            databaseHelper.removeContact(phoneNO.replace(" ", ""));
        }
    }
}

Now the problem is that within if(phoneNO.substring(0,3).equals("+92")) condition, the checkbox value is not saved but the items data is saved in SQLite. But in else part its working fine. How can I solve this problem

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                item.setIgnored(isChecked);
                if (listener != null){
                    listener.onCheckedChangeListener(item.getPhoneNumber() , isChecked);
                }
            }
        });




Aucun commentaire:

Enregistrer un commentaire