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 the 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.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
db.collection("storage")
.get()
.addOnSuccessListener { result ->
for (document in result) {
Log.d("exist", "${document.id} => ${document.data}")
value1.text = document.getString("value1")
value2.text = document.getString("value2")
value3.text = document.getString("value3")
}
}
.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" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="383dp"
android:layout_height="wrap_content"
android:layout_marginTop="34dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="1dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your search here..." />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Error message for new error:
Error in the code:
Solution
Assuming that you have a database structure that looks like this:
Firestore-root
|
--- storage (collection)
|
--- apple (document)
| |
| --- value1: "v1"
| |
| --- value2: "v2"
| |
| --- value3: "v3"
|
--- banana (document)
|
--- value1: "v1"
|
--- value2: "v2"
|
--- value3: "v3"
To get the values of the three properties, for example, for the "apple" document, please use the following lines of code:
val rootRef = FirebaseFirestore.getInstance()
val storageRef = rootRef.collection("storage")
storageRef.document("apple").get().addOnSuccessListener { document ->
if (document != null) {
val v1 = document.getString("value1")
value1.text = v1
val v2 = document.getString("value2")
value2.text = v2
val v3 = document.getString("value3")
value3.text = v3
Log.d(TAG, v1 + "/" + v2 + "/" + v3)
} else {
Log.d(TAG, "No such document")
}
}.addOnFailureListener { exception ->
Log.d(TAG, "get failed with ", exception)
}
So you have to specify the name of the collection as well the name of the document when building the reference. The output in your logcat will be:
v1/v2/v3
Answered By - Alex Mamo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.