猿问

如何在 Firebase 实时数据库中选择键名?

如何选择 Firebase 实时数据库中的密钥名称?

这是我的数据库

这里的key是自动生成的(-LiNph8fnLlcDTkLqq_K)。我该如何选择它的名字?

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
myRef.child("Data").push().setValue("some value");


函数式编程
浏览 76回答 2
2回答

慕丝7291255

push()方法将生成一个随机 ID。如果您想选择自己的 ID,则可以执行以下操作:FirebaseDatabase database = FirebaseDatabase.getInstance();DatabaseReference myRef = database.getReference();myRef.child("Data").child("firstData").child("value").setValue("some value");然后你会:Data  firstData      value : some value但是最好使用,因为您的数据库中push()不能有相同的两次。

幕布斯7119047

fun getListFromFirebaseDbByID(formDataID : String, formListener: SingleFormDataDBListener) {&nbsp; &nbsp; &nbsp; &nbsp; val database = FirebaseDatabase.getInstance().reference&nbsp; &nbsp; &nbsp; &nbsp; val rootRef = database.child(Constant.FIREBASE_DATABASE)&nbsp; &nbsp; &nbsp; &nbsp; rootRef.addListenerForSingleValueEvent(object : ValueEventListener {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override fun onCancelled(p0: DatabaseError) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override fun onDataChange(snapshot: DataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val children = snapshot!!.children.iterator()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (children.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val currentItem = children.next()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val map = currentItem.getValue() as HashMap<String, Any>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val id = map.get("id") as String?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答