Search code examples
androidfirebasefirebase-realtime-databasefirebaseui

Delete item from the Firebase database


I'm trying to delete an item from the following list. But when I try to grab the key, it shows the user id key which is NsShnb2enJcYEkfeE2fiSzbDc6O2. But I want to grab the key of each post. For example, if the user clicks on the first item in the list, it should grab the key KSGhFSZg....

There are lots of answers to the similar questions but the structure of the database is quite different here.

enter image description here

Here is the code, I used.

mAdapter = new FirebaseRecyclerAdapter<Note, FirebaseNoteViewHolder>(Note.class, R.layout.item_note, FirebaseNoteViewHolder.class, mRef) {
    @Override
    public void populateViewHolder(FirebaseNoteViewHolder noteMessageViewHolder, final Note noteMessage, final int position) {
        noteMessageViewHolder.setTitle(noteMessage.getTitle());
        noteMessageViewHolder.setUpdatedDate(DateFormat.getInstance().format(noteMessage.getDataModified()));

        noteMessageViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getActivity(), noteMessage.getDataModified().toString(), Toast.LENGTH_LONG).show();

                databaseReference.orderByChild("dataModified").equalTo(noteMessage.getDataModified().toString()).addListenerForSingleValueEvent(
                        new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {

                                String key = dataSnapshot.getKey();

                                Toast.makeText(getActivity(), key, Toast.LENGTH_LONG).show();
                            }


                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
            }
        });
    }
};   

enter image description here

Thanks in advance!


Solution

  • You are getting the parent key so you can try:

          databaseReference.orderByChild("dataModified").equalTo(noteMessage.getDataModified().toString()).addListenerForSingleValueEvent(
              new ValueEventListener() {
               @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
    
                String key = dataSnapshot.getKey();
                int positionCount=0;
                for(DataSnapshot note : dataSnapshot.getChildren()){
                    if(positionCount==position){
                       firebase.child("notes").child(key).child(note .getKey()).removeValue();
                       Toast.makeText(getActivity(), note.getKey(), Toast.LENGTH_LONG).show();
                       }
                   positionCount++;
                }
    
    
          }
          @Override
          public void onCancelled(DatabaseError databaseError) {
    
                    }
         });