I'm using firebase real-time database I'm using checkboxes to add wen checked and deleted when unchecked like this :
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
switch (compoundButton.getId()) {
case R.id.borrowing:
if (b)
{ addToList("borrowing");
Log.i("checked","b is"+b);
} else {
deleteFromList("borrowing",b);
Log.i("unchecked","b is"+b);
}
break;
case R.id.reading:
if (b)
{addToList("toRead");
}else{
deleteFromList("toRead",b);
}
break;
case R.id.lending:
if (b){addToList("lending");
}else {
deleteFromList("lending",b);
}
break;
}
}
and I add data to fire base like this :
public void addToList(String id ){
df.child(uid).child(id).push().setValue(Id).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
Toast.makeText(getContext(),"Your data is updated",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getContext(),task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});}
and deletedata like this :
public void deleteFromList(String id, final boolean b){
df.child(uid).child(id).orderByChild(Id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!b) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
if (dataSnapshot1.getValue().equals(Id)) {
dataSnapshot1.getRef().removeValue();
Toast.makeText(getContext(),"Your data is deleted",Toast.LENGTH_LONG).show();
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
added, }
the problem is when I add data after the addvaluelister is attached the the data is immediately deleted after being added , what is the problem here and how to fix that ??
Aucun commentaire:
Enregistrer un commentaire