Issue
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)
}
}
}
Solution
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
Answered By - Frank van Puffelen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.