0

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

jawaher
  • 25
  • 6
  • 1
    post the code not image. Also not getting what you actually want.if you want to delete anything inside POST first u need to get the key. Key you can get by searching with any unique variable like postID. get key(ajskshsksj something like this) from post id then like this DatabaseReference dR = FirebaseDatabase.getInstance().getReference("POst").child(key you get); dR.removeValue(); – bhaskarkh Apr 02 '22 at 13:12
  • I update my question – jawaher Apr 04 '22 at 12:44

2 Answers2

2

There is no removing code line in your screenshot.

reference2.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
       dataSnapshot.getRef.removeValue();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        //for database error
    }
});

Or simply,

reference2.removeValue();

Don't post your code as image in the next time.

pak
  • 55
  • 5
0

Simply do this

reference2.child(YOUR_ITEM_KEY).setValue(null);
Kashif Masood
  • 286
  • 1
  • 6