所以,我分配了我的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)
}
})
答案 0 :(得分:0)
嘿Andy我还没有使用过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初始化为可空属性