0

I am trying to build two an autotextView that should be passed on to firebase to look for the according documentId there to show 3 values of that document.

My problem is that I cannot connect the documentId of the first part (autoTextView) to the second part (firebase query). The two individual parts work fine, but the connection between the two trough the documentId string doesnt seem to work.

My problem is that I dont know how I can call the document name (saved as "enteredText")

in line

    val docRef = db.collection("strecken").document("enteredText")

Does anyone know what the problem is/how I can make it work?

    package com.example.servus
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.util.Log
    import android.widget.TextView
    import com.google.firebase.firestore.ktx.firestore
    import com.google.firebase.ktx.Firebase
    
    import android.view.View
    import android.widget.ArrayAdapter
    import android.widget.AutoCompleteTextView
    import android.widget.Button
    import android.widget.Toast
    import com.google.firebase.firestore.FieldPath.documentId
    
    
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        // autotextView for Start

        val autotextView
                = findViewById<AutoCompleteTextView>(R.id.autoTextViewStart)
        val autotextViewZiel
                = findViewById<AutoCompleteTextView>(R.id.autoTextViewZiel)


    // Get the array of languages
    val languages
            = resources.getStringArray(R.array.Languages)
    // Create adapter and add in AutoCompleteTextView
    val adapter
            = ArrayAdapter(this,
        android.R.layout.simple_list_item_1, languages)
    autotextView.setAdapter(adapter)
    autotextViewZiel.setAdapter(adapter)


    val button
            = findViewById<Button>(R.id.btn); if (button != null)
    {
        button.setOnClickListener(View.OnClickListener {
            val enteredText = autotextView.getText()
            Toast.makeText(this@MainActivity, enteredText, Toast.LENGTH_SHORT).show()
        })
    }


        // get values from firestore

        val db = Firebase.firestore

        val value1= findViewById(R.id.value1) as TextView
        val value2= findViewById(R.id.value2) as TextView
        val value3= findViewById(R.id.value3) as TextView
        val docRef = db.collection("strecken").document("enteredText")


        //** old hard coded way** val docRef = db.collection("storage").document("apple")
        val docRef = db.collection("storage").document(documentId)

        docRef.get()
           .addOnSuccessListener { document ->
               if (document !=null) {
                    Log.d("exist", "DocumentSnapshot data: ${document.data}")

                   value1.text = document.getString("value1")
                   value2.text = document.getString("value2")
                   value3.text = document.getString("value3")
               } else {
                   Log.d("noexist", "No such docoument")
               }
           }
           .addOnFailureListener { exception ->
                Log.w("notexisting", "Error getting documents.", exception)
           }
    }
}

1 Answer 1

1

You'll need to pull the enteredText variable up, so that it becomes available in the code where you need it:

var enteredText: String // 👈 Add this declaration

val button
        = findViewById<Button>(R.id.btn); if (button != null)
{
    button.setOnClickListener(View.OnClickListener {
        enteredText = autotextView.getText().toString() // 👈 assign (but don't declare) it here
        Toast.makeText(this@MainActivity, enteredText, Toast.LENGTH_SHORT).show()
    })
}


// get values from firestore

val db = Firebase.firestore

val value1= findViewById(R.id.value1) as TextView
val value2= findViewById(R.id.value2) as TextView
val value3= findViewById(R.id.value3) as TextView
val docRef = db.collection("strecken").document(enteredText) // 👈 So that you can use it here
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help! I have done that but know I get the error "Captured values initialization is forbidden due to possible reassignment" for "entered Text [= ...] and "Type mismatch. Required: String Found: Editable!" for [... =] "autotextView.getText()". Do you know what the problem could be?
Ah yeah, it seems the variable should be a var and not a val. Btw: I highly recommend searching for an error message like that, as I got the solution to this error from that too.
Thanks, I corrected the two errors now so its: "var enteredText: String" and "enteredText = autotextView.getText().toString()".The only remaining problem is that my database referemce now says that "Variable 'enteredText' must be initialized". Do you know what I could do to fix that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.