Issue
my code to delete one item in firebase in child recipe the method deletes all the list item in the recipe on click one item I think that because for loop the get all item not specific item and delete all of them I don't know how can delete just one item in the list key I went delete the one key just one on click button delete enter image description here
the recipe reference has two keys when click delete button delete all key not one
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(view.getRootView().getContext());
builder1.setTitle("Delete post");
builder1.setMessage("Are you sure that you want delete the post?");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, final int id) {
ref.child("recipe").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()){
String skey = uniqueKeySnapshot.getKey();
uniqueKeySnapshot.child(String.valueOf(id)).child(skey).getRef().removeValue(); //ref.child("recipe").child(skey).setValue(null);
dialog.cancel();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(mContext, "error !"+databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
dialog.cancel();
}
});
builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
});
i make for loop in chiled bacuse have list recipe
Solution
Simply do this
reference2.child(YOUR_ITEM_KEY).setValue(null);
Answered By - Kashif Masood
Answer Checked By - Senaida (JavaFixing Volunteer)