【问题标题】:How to replace string-arrays with firestore references in Android Kotlin?如何在 Android Kotlin 中用 Firestore 引用替换字符串数组?
【发布时间】:2021-09-05 10:11:32
【问题描述】:

我正在尝试使用 firestore 连接构建 autoTextView。

我已经构建了一个可以工作的 autoTextView 应用程序,其中数据直接保存在 strings.xml 文件中。我也已经将 Firestore 连接到我的应用。

我现在要做的是将我的 strings.xml 文件中的这些数据替换为与 firebase 的连接。所以用户在 autoTextView 中输入了一些东西,自动完成选项应该来自 firebase 而不是来自 strings.xml 文件。

有人知道我可以如何尝试实现它吗?

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 文件:

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>

【问题讨论】:

    标签: android firebase kotlin google-cloud-firestore


    【解决方案1】:

    获得语言后,您需要从 firestore 数据库中获取它们,而不是从 strings.xml 加载它们。例如,如果您处于离线状态,您可以使用 strings.xml 值作为备用。

    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.
            }
    

    这是一个异步操作,可能需要几秒钟。您应该考虑在用户等待的同时向他们展示什么。以后的时间应该使用本地 Firestore 缓存来避免等待。阅读有关 Firestore Android SDK 的更多信息以了解如何使用它。

    【讨论】:

    • 感谢您的帮助!如果我继续通过“strings.xml”文件做所有事情,你认为如果我有超过几百个字符串也会出现性能问题?
    • strings.xml 仅适用于您应用中的静态文本,例如您拥有的 &lt;string name="hint"&gt;Please type language...&lt;/string&gt;。您可以在某种程度上格式化字符串以在使用它们时插入数字,但仅此而已。这使得将所有字符串翻译成其他语言变得非常容易,这就是您最终为您的应用支持的每种语言提供多个strings.xml 的方式。对于状态依赖于数据库的动态文本,您应该始终只从数据库中加载它们,这通常会自动处理缓存。然后需要手动处理翻译。
    猜你喜欢
    • 1970-01-01
    • 2013-08-17
    • 2013-11-08
    • 1970-01-01
    • 2014-03-21
    • 2021-08-25
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    相关资源
    最近更新 更多