我想从数据库中获取用户名和他的电子邮件,我用它来将数据设置为数据库:
createUser
fun createUser( name:String, email: String, password: String) {
mProgressbar.setMessage("Please wait..")
mProgressbar.show()
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
val user = User(
name,
email
)
FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().currentUser!!.uid)
.setValue(user).addOnCompleteListener(OnCompleteListener<Void> { task ->
if (task.isSuccessful) {
Toast.makeText(this, "Registered Successfully", Toast.LENGTH_LONG).show()
val Intent = Intent(applicationContext, MainActivity::class.java)
startActivity(Intent)
finish()
mProgressbar.dismiss()
} else {
//display a failure message
}
})
} else {
Toast.makeText(this, "Authentication failed.${task.exception}", Toast.LENGTH_SHORT).show()
mProgressbar.dismiss()
}
}
}
用户
class User ( name: String, email: String) {
private var name: String? = name
private var email: String? = email
fun setName(Name: String) {
this.name = Name
}
fun getName(): String? {
return name
}
fun setEmail(Email: String) {
this.email = Email
}
fun getEmail(): String? {
return email
}
}
我需要帮助。我需要帮助。我需要帮助。我需要帮助。我需要帮助。我需要帮助。我需要帮助。我需要帮助。我需要帮助。
答案 0 :(得分:1)
internal var user: User? = null // declare user object outside onCreate Method
var ref = FirebaseDatabase.getInstance().getReference("Users").child(FirebaseAuth.getInstance().currentUser!!.uid)
val menuListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
user = dataSnapshot.getValue() as User
textView.text = user?.getName()
}
override fun onCancelled(databaseError: DatabaseError) {
// handle error
}
}
ref.addListenerForSingleValueEvent(menuListener)
答案 1 :(得分:0)
从数据库获取值的代码
val menuListener = object : ValueEventListener {
override fun onCancelled(databaseError: DatabaseError) {
// handle error
}
override fun onDataChange(dataSnapshot: DataSnapshot) {
user = dataSnapshot.getValue(User::class.java)
textView.text = user?.name
}
}
并且我必须将用户类代码更改为:
@IgnoreExtraProperties
data class User(
var name: String? = "",
var email: String? = ""
) {
@Exclude
fun toMap(): Map<String, Any?> {
return mapOf(
"uid" to name,
"author" to email
)
}
}