0

I am trying to build an autoTextView with a firestore connection.

I already built a working autoTextView application in which the data is saved directly in the strings.xml file. I also already connected firestore to my app.

What I am trying to do now is to replace this data frommy strings.xml file with a connection to firebase. So the user types something in in the autoTextView, the auto completion options should come from firebase and not from the strings.xml file.

Does anyone have an idea how I could try implementing that?

strings.xml:

<resources>
    <string name="app_name">servus</string>
    <string name="hint">Please type language...</string>
    <string name="submit">Submit</string>
    <string name="submitted_lang">Submitted language:</string>

    <string-array name="Languages">
        <item>Java</item>
        <item>Kotlin</item>
        <item>Swift</item>
        <item>Python</item>
        <item>Scala</item>
        <item>Perl</item>
        <item>Javascript</item>
        <item>Jquery</item>
    </string-array>

</resources>

MainActivity.kt file:

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


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val autotextView
                = findViewById<AutoCompleteTextView>(R.id.autoTextView)
        // 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)

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

activity_main.xml:

<?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">

    <AutoCompleteTextView
        android:id="@+id/autoTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="@string/hint"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.249" />

</androidx.constraintlayout.widget.ConstraintLayout>

1 Answer 1

1

When you get the languages, you need to fetch them from the firestore database instead of loading them from strings.xml. You can use the strings.xml values as a fall-back for if you're offline, for example.

Reading a document from firestore:

// This code goes in your DatabaseManger or whatever class in your android app your handling firestore
// Figure out the appropriate document path based on the DB schema you designed
val docRef = db.collection("collection_name").document("document_name")
docRef.get()
        .addOnSuccessListener { document ->
            if (document != null) {
                Log.d(TAG, "DocumentSnapshot data: ${document.data}")
                // Here's your languages, save them somewhere so you can use them
            } else {
                Log.d(TAG, "No such document")
            }
        }
        .addOnFailureListener { exception ->
            Log.d(TAG, "get failed with ", exception)
            // Probably offline, or auth problem. Maybe use strings.xml backup.
        }

This is an asynchronous operation and may take a couple of seconds. You should think what to show to the user in the meantime while they wait. Future times should use the local Firestore cache to avoid the wait. Read more about the Firestore Android SDK to learn how to use it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help! If I would continue to do everything via the "strings.xml" file, you you think there could also evolve performance problems if I have more than a few hundreds strings?
strings.xml is strictly for static text in your app, like the <string name="hint">Please type language...</string> one you have. You can format the strings to some degree to insert numbers when you use them, but that's about it. This makes it very easily to translate all the strings into other languages, and that's how you end up with multiple strings.xml for each language your app supports. For dynamic text that depends on a database for state, you should always just load them from your database, which usually automatically handles caching. Translations then need to be handled manually.

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.