简体   繁体   中英

How do I allow users to delete their own post from Firebase?

I am trying to implement a feature that allows users to delete their own post from Firebase, but I am unsure on how to go about doing this. I managed to allow the user to remove their post from Recycleview but that doesn't really address the issue. Some help would be greatly appreciated. This is how my database is structured, I am saving users post by calling the push method on a dictionary so a random key is being generated each time a user makes a post.

Post
 -LkKPt_cQta_ZRWHJ6Cw
 desc: 
 id: 
 image: 
 name: 
 profileimage: 


//Creates popup and allows user to delete from RecycleView
public void openOptionMenu(View v,final int position){
    PopupMenu popup = new PopupMenu(v.getContext(), v);
    popup.getMenuInflater().inflate(R.menu.options_menu, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu1:
                    Toast.makeText(getApplicationContext(), "Edit clicked", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.menu2:
                    String logedInUser = currentUser.getUid();
                    postList.remove(position);
                    adapter.notifyDataSetChanged();
                    return true;
                default:
                    //default intent
                    return true;
            }
        }
    });
    popup.show();
}

well, it depends on how your database is structured. let's say every post has its own id you can use this:

    FirebaseDatabase.getInstance().getReference().child("nameOfTheNode").child("postId").removeValue();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM