0

I am storing user post data in firebase realtime database and displaying those post in a list view. I have set the list view on button click listener so that user can delete it if he/she wants. I have used push(), so random key is generating when user making new post. Now i am not being able to delete it. I have also tried to put that key as child also, but i am not finding any way how to delete it. Any help is much appreciated.

I want this "-LX5Db4_suS_wPjzRDLE" to be deleted only when user will click this post from the list view

I want this "-LX5Db4_suS_wPjzRDLE" to be deleted only when user will click this post from the list view. As i am using this to display in user post history so i can only get the 1st child using getUid() for current user.

Here is the code i have tried,

 FirebaseDatabase database=FirebaseDatabase.getInstance();
final DatabaseReference ref=database.getReference("Donation Post").child(user.getUid());

ref.removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {

        if (task.isSuccessful()){
            Toast.makeText(UserPostHistoryActivity.this, "Post Deleted Successfully", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(UserPostHistoryActivity.this, "Post Not Deleted!!!", Toast.LENGTH_SHORT).show();

        }
    }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
SA Sajib
  • 67
  • 1
  • 8

1 Answers1

0

To delete an item under the user node, you will need to know the key of that item. So for example to delete item -LX5Db4_suS_wPjzRDLE, you'd need:

database.getReference("Donation Post").child(user.getUid()).child("-LX5Db4_suS_wPjzRDLE").removeValue();

If the items are in a list and you want to delete the item that the user clicked on, you'll typically have a OnItemClickListener and your OnItemClick will be told the position of the item the user clicked on. You'll need to map this position back to a key, and then run the code above.

If you're using FirebaseUI's built-in adapter to show the items in the ListView, you can get a reference to the clicked item as shown here: How to remove currently selected item in ListView that works with FirebaseListAdapter?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you sir for your suggestion but I actually wanted to know how to get this key "-LX5Db4_suS_wPjzRDLE" to delete it when user will click this item from list view. – SA Sajib Jan 28 '19 at 00:58
  • Your question shows none of the code related to how you get the items in the list view, so I gave both options in my answer. If you've built your own adapter, you'll have to add a way to map from the position that the user clicked on back to the key. If you're using FirebaseUI, the answer I linked shows how to do it there. – Frank van Puffelen Jan 28 '19 at 01:05
  • Thank you sir, Somehow now i can delete it. I retrieve the child unique_key of the post while clicking the post from list view and used that key as, ref.child(key).removeValue() to delete it. – SA Sajib Jan 28 '19 at 03:09