简体   繁体   中英

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

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

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)
}

You will have to get the document to populate the fields, you simply set the text value AFTER your get request resolves as it is not instant.

Per the documentation:

db.collection("users")
        .get()
        .addOnSuccessListener { result ->
            if(result != null) {
                binding.edtEditName.setText(result.getString("Name"))
            }
        }
        .addOnFailureListener { exception ->
            Log.w(TAG, "Error getting documents.", exception)
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM