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:


