1

因此,我分配了我的 var spinner 数组以从我的 firestore 文档中获取数据,但这就是结果我得到的。

这是我目前的活动。如果我分配字符串,我可以让它工作,但最终我想从我的文档中加载数据,就像我现在正在尝试做的那样。

这是我的火库设置。

有什么理由吗?谢谢

var db = FirebaseFirestore.getInstance()



var spinnerArray = arrayOf(db.collection("KitList")
           .get()
           .addOnCompleteListener { task ->
               if (task.isSuccessful) {
                   for (document in task.result) {
                       Log.d("weight05", document.id + " => " + document.data)
                   }
               } else {
                   Log.w("error", "Error getting documents.", task.exception)
               }
           })

var newKitList = mutableListOf<String>()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_new_kit_list)
    var addButton = addKitItemBtn

    val spinner = newKitItemSpinner
    val spinnerArrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray)
    //selected item will look like a spinner set from XML
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    spinner.adapter = spinnerArrayAdapter

    val kitList = newKistListView
    val listViewAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, newKitList)
    kitList.adapter = listViewAdapter

    addButton.setOnClickListener(object : View.OnClickListener {
        override fun onClick(v: View?) {
            val selectedItem = spinner.selectedItem.toString()
            newKitList.add(selectedItem)
            listViewAdapter.notifyDataSetChanged()
        }
    })

===============

编辑

var spinnerArray: Array<Task<QuerySnapshot>> = arrayOf(db.collection("KitList").get()
       .addOnCompleteListener { task ->
           if (task.isSuccessful) {
               spinnerArray = task.result as Array<Task<QuerySnapshot>> //or however you get your document array
               for (document in task.result) {
                   Log.d("weight05", document.id + " => " + document.data)
               }
           } else {
               Log.w("error", "Error getting documents.", task.exception)
           }
       })
4

1 回答 1

0

嘿,安迪,我还没有使用过 FireStore,但有使用 Firebase 数据库和 Kotlin 的经验。根据我的经验,我可以清楚地告诉你,你不会直接收到你的数组,因为数据的获取不是在主线程上完成的。目前,您的 spinnerArray 持有对 Firebase 任务的引用,这就是您的微调器显示此类文本的原因。

要将数组放入 spinnerArray 变量中,您需要在任务成功时获取它,即有点像这样

编辑: 删除您的 var spinnerArray 初始化并按照我添加微调器的适配器进行操作。

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new_kit_list)
var addButton = addKitItemBtn

val spinner = newKitItemSpinner
val spinnerArrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray)
//selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = spinnerArrayAdapter

val kitList = newKistListView
val listViewAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, newKitList)
kitList.adapter = listViewAdapter

addButton.setOnClickListener(object : View.OnClickListener {
    override fun onClick(v: View?) {
        val selectedItem = spinner.selectedItem.toString()
        newKitList.add(selectedItem)
        listViewAdapter.notifyDataSetChanged()
    }
})
db.collection("KitList")
       .get()
       .addOnCompleteListener { task ->
           if (task.isSuccessful) {
               spinnerArray = task.result //or however you get your document array
               var spinnerArray = task.result as Array<Task<QuerySnapshot>>
               val spinnerArrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray)
               //selected item will look like a spinner set from XML
               spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
               spinner.adapter = spinnerArrayAdapter
               for (document in task.result) {
                   Log.d("weight05", document.id + " => " + document.data)
               }
           } else {
               Log.w("error", "Error getting documents.", task.exception)
           }
       }
}

在 onCreate 中调用这段代码并将 spinnerArray 初始化为可为空的属性

于 2017-10-04T13:41:02.950 回答