Issue
I am trying to build a little app where the user can find values of documents.
As a database I am using Firestore Database. I only have one collection "storage" with many documents and 3 value fields per document. I already built an activity that gets the 3 value fields from the database (see below).
What I am trying to do now is to let the user decide which values he wants to see by selecting different database document names.
For example if the user types in "apple" in a text input field, I want to show him the 3 values that I have saved in the collection "storage" -> document "apple" -> fields "value1", "value2" and "value3". If the user types in "banana" in the text input field, I want to show him the 3 values that I have saved in the collection "storage" -> document "banana" -> fields "value1", "value2" and "value3".
Could anyone give me a few tips how to implement that? Unfortunately I couldnt find much help elsewhere.
MainActivity.kt
package com.example.test
import android.content.ContentValues.TAG
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
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("storage").document("apple")
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)
}
}
}
activity_main.xml (looks horrible, but for now I just wanna work out the functionality)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/value1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.585" />
<TextView
android:id="@+id/value2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.873"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.585" />
<TextView
android:id="@+id/value3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.162"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.586" />
</androidx.constraintlayout.widget.ConstraintLayout>
Solution
You'll need to introduce a new field where the user can enter or select the document name. Once you have that, you can read its value and use that when accessing the database with something like this:
...
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 documentId = (findViewById(R.id.documentId) as TextView).getText().toString()
// 👆 Get the value the user entered here
val docRef = db.collection("storage").document(documentId)
// 👆 Then use it here
docRef.get()
.addOnSuccessListener { document ->
...
Answered By - Frank van Puffelen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.