如何通过 Kotlin 中的数据库模型 class 将键作为值传递
How to pass key as a value through a database model class in Kotlin
这是我的数据库的样子:
Realtime Database Layout
这是我的模型class:
class Registrations {
private var userEmail:String =""
private var verified:Boolean = false
constructor()
constructor(userEmail:String, verified:Boolean){
this.userEmail = userEmail
this.verified = verified
}
fun getUserEmail():String{
return userEmail
}
fun setUserEmail(userEmail: String){
this.userEmail = userEmail
}
fun getVerified():Boolean{
return verified
}
fun setVerified(verified: Boolean){
this.verified = verified
}
}
我正在使用 RecyclerAdapter 获取数据,我的代码是:
private fun getRegistrations() {
FirebaseDatabase.getInstance().reference.child("Registrations").addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
if (dataSnapshot.exists()) {
itemList!!.clear()
for (snapshot in dataSnapshot.children) {
Log.d("ITEM", snapshot.toString())
val item = snapshot.getValue(Registrations::class.java)
itemList!!.add(item!!)
}
registrationsAdapter!!.notifyDataSetChanged()
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
}
现在,正如您在数据库图像中看到的那样,我如何将键值(用户的电子邮件)作为值传递给模型 class?
我已经尝试创建一个对象,但我正在做一些事情wrong/don不知道该怎么做,感谢您的帮助。
此外,此应用程序正在生产中,因此我现在无法更改数据以将电子邮件作为 firebase 中的值。
感谢任何帮助。
始终建议 class 中的字段必须与数据库中的字段相匹配。因此,如果数据库中的一个节点包含两个字段,verified
和 activationCode
,那么 class 应该包含相同的字段。
this app is in production so I can't change the data to have email as a value in fFirebase now.
因此,如果您不能将电子邮件作为值添加,我怀疑这是一个非常简单的更新操作,那么您只能读取密钥并使用 setter 添加到您的对象中,如下所示:
val item = snapshot.getValue(Registrations::class.java)
val userEmail = snapshot.getKey()
item.setUserEmail(userEmail)
itemList!!.add(item!!)
这是我的数据库的样子:
Realtime Database Layout
这是我的模型class:
class Registrations {
private var userEmail:String =""
private var verified:Boolean = false
constructor()
constructor(userEmail:String, verified:Boolean){
this.userEmail = userEmail
this.verified = verified
}
fun getUserEmail():String{
return userEmail
}
fun setUserEmail(userEmail: String){
this.userEmail = userEmail
}
fun getVerified():Boolean{
return verified
}
fun setVerified(verified: Boolean){
this.verified = verified
}
}
我正在使用 RecyclerAdapter 获取数据,我的代码是:
private fun getRegistrations() {
FirebaseDatabase.getInstance().reference.child("Registrations").addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
if (dataSnapshot.exists()) {
itemList!!.clear()
for (snapshot in dataSnapshot.children) {
Log.d("ITEM", snapshot.toString())
val item = snapshot.getValue(Registrations::class.java)
itemList!!.add(item!!)
}
registrationsAdapter!!.notifyDataSetChanged()
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
}
现在,正如您在数据库图像中看到的那样,我如何将键值(用户的电子邮件)作为值传递给模型 class?
我已经尝试创建一个对象,但我正在做一些事情wrong/don不知道该怎么做,感谢您的帮助。
此外,此应用程序正在生产中,因此我现在无法更改数据以将电子邮件作为 firebase 中的值。
感谢任何帮助。
始终建议 class 中的字段必须与数据库中的字段相匹配。因此,如果数据库中的一个节点包含两个字段,verified
和 activationCode
,那么 class 应该包含相同的字段。
this app is in production so I can't change the data to have email as a value in fFirebase now.
因此,如果您不能将电子邮件作为值添加,我怀疑这是一个非常简单的更新操作,那么您只能读取密钥并使用 setter 添加到您的对象中,如下所示:
val item = snapshot.getValue(Registrations::class.java)
val userEmail = snapshot.getKey()
item.setUserEmail(userEmail)
itemList!!.add(item!!)