Home > front end >  Firebase to Kotlin Fragment
Firebase to Kotlin Fragment

Time:09-25

Is there any way to retrieved selected data in Firebase to a fragment in Kotlin? Not all data only selected row.

I'm new to Kotlin. Please help me.

firstly user login to the system. for this I checked the user name from Firebase. it works properly. But after that, I want to retrieve data of that username into a fragment. In one activity I add five fragments. I need to load all data of that user into one Fragment.

for example in this abi is a username and I need to load all data of only that username into one fragment.

Firebase data Firebase data

Inside of abi Inside of abi

here's what I added.

val rootRef = FirebaseDatabase.getInstance().reference
        val userRef = rootRef.child("User").child(sessionId)
        val valueEventListener = object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {

                val name = dataSnapshot.child("fullName").getValue(String::class.java)
                val age = dataSnapshot.child("age").getValue(String::class.java)
                val phone = dataSnapshot.child("phoneNo").getValue(String::class.java)
                val bank = dataSnapshot.child("bankAccNo").getValue(String::class.java)
                val password = dataSnapshot.child("passwordRegister").getValue(String::class.java)
                    nameText.setText(name)
                    ageText.setText(age)
                    phoneText.setText(phone)
                    bankText.setText(bank)
                    passwordText.setText(password)

            }

            override fun onCancelled(error: DatabaseError) {
                Log.d("TAG", error.getMessage())

            }
        }
        userRef.addListenerForSingleValueEvent(valueEventListener)

CodePudding user response:

To get the data from the Firebase Realtime Database that corresponds only to the abi user, please use the following lines of code:

val rootRef = FirebaseDatabase.getInstance().reference
val userRef = rootRef.child("User").child("abi")
val valueEventListener = object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        val name = dataSnapshot.child("fullName").getValue(String::class.java)
        Log.d("TAG", name)
    }

    override fun onCancelled(databaseError: error) {
        Log.d("TAG", error.getMessage()) //Don't ignore potential errors!
    }
}
userRef.addListenerForSingleValueEvent(valueEventListener)

In the same way, you can also get the values of the other fields. The result in the logcat will be:

Abilashini
  • Related