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.

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) {

                            }
                        });
            }
        });
    }
};   

Thanks in advance!

Answers


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) {

                }
     });

for getting your key you should to give full path to your request. In your case

databaseReference.child("notes" + "/" + yourUserId ).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) {

                        }
                    });

Need Your Help

How to change Facebook 'Like' button to 'Recommend' button

javascript facebook iframe

I have a snippet to create a 'Like' button for our news site:

iOS - XMPP with WebRTC for realtime video call or chat

ios objective-c video xmpp webrtc

I have searched the whole web but did not get any document for passing the XMPP IQ to WebRTC. Also I see is the XMPP Jingle class, but cannot find any document for integrate this.