I am trying to setup a profile page in my project, and I am trying implement a function that when the user clicks the profile tab on the bottom, it will display the info that is stored on my project’s database after being redirected to the profile page.
Here is the code for my profile page:
class User_Activity : AppCompatActivity() {
private lateinit var binding: ActivityUserBinding
private lateinit var databaseReference: DatabaseReference
private lateinit var firebaseAuth: FirebaseAuth
private lateinit var uid : String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityUserBinding.inflate(layoutInflater)
setContentView(binding.root)
loadUserDetails()
firebaseAuth = FirebaseAuth.getInstance()
uid = firebaseAuth.currentUser?.uid.toString()
databaseReference = FirebaseDatabase.getInstance().getReference("Users")
val LogoutBtn = findViewById<Button>(R.id.logoutBtn)
LogoutBtn.setOnClickListener {
val intent = Intent(this@User_Activity, MainActivity::class.java)
startActivity(intent)
}
//navbar redirection
val addImg: ImageView = findViewById(R.id.navCreate)
addImg.setOnClickListener {
val intent = Intent(this@User_Activity, Slam_Activity::class.java)
startActivity(intent)
}
val gotoUser: ImageView = findViewById(R.id.navUser)
gotoUser.setOnClickListener {
val intent = Intent(this@User_Activity, User_Activity::class.java)
startActivity(intent)
}
val gotoHome: ImageView = findViewById(R.id.navHome)
gotoHome.setOnClickListener {
val intent = Intent(this@User_Activity, Homepage_Activity::class.java)
startActivity(intent)
}
}
here is the function I am trying to implement:
private fun loadUserDetails() {
//db ref to fetch user info
val userRef = databaseReference.child(uid)
userRef
.addValueEventListener(object: ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
for (usersnapshot in snapshot.children) {
//Log.d("FirebaseData", "Snapshot: $snapshot")
val email = usersnapshot.child("email").value.toString()
val username = usersnapshot.child("username").value.toString()
val uid = usersnapshot.child("id").value.toString()
//Log.d("FirebaseData", "Email: $email, Username: $username, UID: $uid")
binding.profileUID.text = uid
binding.profileEmail.text = email
binding.profileUsername.text = username
}
}
override fun onCancelled(error: DatabaseError) {
Log.e("FirebaseError", "Error: $error")
}
})
}
This is what my database looks like:
what happens now after clicking the profile tab on the nav bar is that it loops back to the sign in page and when you click the profile tab again, it crashes.
I have tried to do different codes, but I am still getting nothing, even after researching numerous articles. The output should be returning all the data from the current user id.
2
Answers
You’re loading data from
/Users/$userId
, so theDataSnapshot
you get in youronDataChange
will have the information for a single user.But the code in your
onDataChange
loops over the child nodes of the data snapshot, as if it gets a list of results. That won’t work, which probably explains the crash.The solution is to remove the loop from your
onDataChange
implementation. Such a loop is only needed when you get a list of results, not when you directly get the snapshot of a single user node.Can you try this one:
I’ve just added a check if the user is logged in.
Then I used
addListenerForSingleValueEvent
instead ofaddValueEventListener
to fetch data once instead of listening for all data changes.