英文:
save unique key firebase to child
问题
以下是已翻译的内容:
我有一个像这样的数据库
"Chats" : {
"-MCzU6r-JqvAkC9unjru" : {
"isSeen" : false,
"message" : "H",
"receiver" : "wwRWdMQ62bNwO09M5LpxXks37442",
"sender" : "R5ICmEp5B6Sbhow523BmQoF6uUG2",
"timestamp" : "1595571732585",
"type" : "text"
}}
我想要将唯一键"-MCzU6r-JqvAkC9unjru"保存在我的子项中,就像这样
"Chats" : {
"-MCzU6r-JqvAkC9unjru" : {
"isSeen" : false,
"message" : "H",
"receiver" : "wwRWdMQ62bNwO09M5LpxXks37442",
"sender" : "R5ICmEp5B6Sbhow523BmQoF6uUG2",
"timestamp" : "1595571732585",
"type" : "text",
"key" : "-MCzU6r-JqvAkC9unjru"
}}
我可以这样做吗?如果可以,我该如何在我的代码中实现它?
这里是向 Firebase 添加数据的代码
private void sendMessage(final String message) {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
String timestamp = String.valueOf(System.currentTimeMillis());
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("sender", myUid);
hashMap.put("receiver", hisUid);
hashMap.put("message", message);
hashMap.put("timestamp", timestamp);
hashMap.put("isSeen", false);
hashMap.put("type", "text");
databaseReference.child("Chats").push().setValue(hashMap);
}
英文:
I have a database like this
"Chats" : {
"-MCzU6r-JqvAkC9unjru" : {
"isSeen" : false,
"message" : "H",
"receiver" : "wwRWdMQ62bNwO09M5LpxXks37442",
"sender" : "R5ICmEp5B6Sbhow523BmQoF6uUG2",
"timestamp" : "1595571732585",
"type" : "text"
}}
I want to save a unique key "-MCzU6r-JqvAkC9unjru" in my child, like this
"Chats" : {
"-MCzU6r-JqvAkC9unjru" : {
"isSeen" : false,
"message" : "H",
"receiver" : "wwRWdMQ62bNwO09M5LpxXks37442",
"sender" : "R5ICmEp5B6Sbhow523BmQoF6uUG2",
"timestamp" : "1595571732585",
"type" : "text",
"key" : "-MCzU6r-JqvAkC9unjru"
}}
can I do that? if can how I can implement it in my code?
here code for add data to firebase
private void sendMessage(final String message) {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
String timestamp = String.valueOf(System.currentTimeMillis());
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("sender", myUid);
hashMap.put("receiver", hisUid);
hashMap.put("message", message);
hashMap.put("timestamp", timestamp);
hashMap.put("isSeen", false);
hashMap.put("type", "text");
databaseReference.child("Chats").push().setValue(hashMap);
答案1
得分: 2
你可以按照以下方式操作:
String key = databaseReference.child("Chats").push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("sender", myUid);
hashMap.put("receiver", hisUid);
hashMap.put("message", message);
hashMap.put("timestamp", timestamp);
hashMap.put("isSeen", false);
hashMap.put("type", "text");
hashMap.put("key", key);
databaseReference.child("Chats").child(key).setValue(hashMap);
使用 getKey()
可以获取使用 push()
创建的随机 ID,然后将它添加到 hashMap
中。
英文:
You can do the following:
String key = databaseReference.child("Chats").push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("sender", myUid);
hashMap.put("receiver", hisUid);
hashMap.put("message", message);
hashMap.put("timestamp", timestamp);
hashMap.put("isSeen", false);
hashMap.put("type", "text");
hashMap.put("key",key);
databaseReference.child("Chats").child(key).setValue(hashMap);
Using getKey()
you can retrieve the random id created using push()
and then add it to the hashMap
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论