因此,我分配了我的 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)
}
})