Pre-written Cloud Firestore data into plain text in android kotlin

Issue

I have a fragment page where users can edit their name and mobile, I wanted the plain text which users can enter data to be pre-written for the second time when they wanted to edit their name, I wanted the name plain text to be pre-written with the data that the user has wrote at the first time

I am currently using cloud Firestore to store my data, as you can see I have already stored data into the Firestore which is name and mobile, I wanted to pre write the data into the plain text which the user can enter data

This is the android kotlin source code for the current edit name fragment

This is the android Kotlin source code for the current edit name fragment

Solution

To get the values of "Mobile" and "Name" that corresponds to the authenticated user and populate the EditText objects, please use the following lines of code:

val uid = FirebaseAuth.getInstance().currentUser?.uid ?: ""
val rootRef = FirebaseFirestore.getInstance()
val usersRef = rootRef.collection("users")
val uidRef = usersRef.document(uid)
uidRef.get().addOnSuccessListener { document ->
    if (document != null) {
         binding.edtEditMobile.setText(document.getString("Mobile"))
         binding.edtEditName.setText(document.getString("Name"))
    } else {
        Log.d(TAG, "No such document")
    }
}.addOnFailureListener { exception ->
    Log.d(TAG, "get failed with ", exception)
}

Answered By – Alex Mamo

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published