This is what my database looks like:
And this is my model 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
}
}
I am using a RecyclerAdapter to get the data and my code to do that is:
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")
}
})
}
}
Now as you can see in the database image how do I pass the key value(email of the user) as a value to the model class?
I have tried creating an object but I am doing something wrong/don't know how to, any help is appreciated.
Also, this app is in production so I can't change the data to have email as a value in firebase now.
Any help is appreciated.
CodePudding user response:
It's always recommended that the fields in the class must match the ones in the database. So if a node in the database contains two fields, verified
and activationCode
, then the class should contain the same.
this app is in production so I can't change the data to have email as a value in fFirebase now.
So if you cannot add the email as a value, which I doubt as it's a really simple update operation, then you can only read the key and use the setter to add in your object like this:
val item = snapshot.getValue(Registrations::class.java)
val userEmail = snapshot.getKey()
item.setUserEmail(userEmail)
itemList!!.add(item!!)