简体   繁体   中英

How to edit the data inside the unique id of a child in firebase database?

Trying to implement a read receipt feature in my app...

Posting Data

private void sendMessage() {
    String messageText = messageArea.getText().toString();
    if (TextUtils.isEmpty(messageText)) {
        Toast.makeText(getApplicationContext(), "Can't Send Blank Message", Toast.LENGTH_SHORT).show();
    } else {

        String message_sender_ref = "Messages/" + MessageSenderId + "/" + MessageRecieverId;
        String message_reciver_ref = "Messages/" + MessageRecieverId + "/" + MessageSenderId;

        Map messageTextBody = new HashMap<>();
        messageTextBody.put("Message", messageText);
        messageTextBody.put("Seen", "False");
        messageTextBody.put("Type", "Text");
        messageTextBody.put("Time", ServerValue.TIMESTAMP);
        messageTextBody.put("From", MessageSenderId);

        DatabaseReference user_message_key = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).push();
        String message_push_id = user_message_key.getKey();

        Map messageBodyDetails = new HashMap();
        messageBodyDetails.put(message_sender_ref + "/" + message_push_id, messageTextBody);
        messageBodyDetails.put(message_reciver_ref + "/" + message_push_id, messageTextBody);

        mDatabaseReference.updateChildren(messageBodyDetails, new DatabaseReference.CompletionListener() {
            @Override
            public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                if (databaseError != null) {
                    Log.d("Chat_Log", databaseError.getMessage().toString());
                }
                messageArea.setText("");
            }
        });
    }
}

Now how do i access the "Seen = False" data which is inside a unique ID... i can access it through ValueEventListener but how do i make changes of that? i know only to fetch the data but i want to change the data of it... Can someone help me out please

Tried method

     DatabaseReference seenRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).child("Seen");
    seenRef.setValue("True");

I tried this above method but it just creates one more hild alongside it and sets its value to true.... someone please help me out... Thanks in advance

Database - https://ibb.co/js3iDd

This method you are trying to use

 DatabaseReference seenRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).child("Seen");
    seenRef.setValue("True");

is replacing or overwriting your current value at that reference, i would suggest to make a map and use updateChildren to just update the value at the desired reference, also if you need to update multiple values at the same reference, this will save you multiple setValues()

Map<String, Object> update = new HashMap<String,Object>();
update.put("Seen", true);
seenRef.updateChildren(update);

but you can also do the same as you are doing without "" in your setValue() because you want to poot a boolean true or false inside it and you are sending a string "True" to the database , this will create a new key replacing/overwriting that same ref with the value Seen : true

DatabaseReference seenRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).child("Seen");
    seenRef.setValue(true);

Please check your seenRef, i just checked your db and i think is missing one more ref

在此处输入图片说明

so just change your ref

DatabaseReference seenRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).child(your_push_id_message).child("Seen");

in order to get your_push_id_message you will need to get that push id of the message to do that, first attach a listener to it and getKey()

seenRef.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
     for(DataSnapshot snapshot : dataSnapshot.getChildren()){

      String pushMessage = snapshot.getKey();
      dataSnapshot.child(pushMessage).child("Seen").setValue(true);



}

  }

  @Override
  public void onCancelled(DatabaseError databaseError) {
    System.out.println("The read failed: " + databaseError.getCode());
  }
});

You are doing wrong because you are the directly updating value not with message-id which you generating for the message using push()

Here you doing

DatabaseReference seenRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).child("Seen");
    seenRef.setValue("True");

But you are not referring message id in which you need to do.

Like bellow

DatabaseReference seenRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).child(Message-Id).child("Seen");
    seenRef.setValue("True");

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