我正在尝试使用Firestore连接构建AutoTextView。
我已经构建了一个工作的自动读取视图应用程序,其中数据将直接保存在strings.xml文件中。我还将FireStore连接到我的应用程序。
我现在想要做的是用一个与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>
mainAdactity.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>
分析解答
当您获取语言时,您需要从Firestore数据库获取它们而不是从strings.xml
加载它们。例如,您可以使用strings.xml
值作为fall-back,例如脱机。
// 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的更多信息,以了解如何使用它。