[当我尝试在微调器中使用动态值时,应用程序崩溃

问题描述 投票:1回答:1

我有以下代码,想知道为什么它会使应用程序崩溃,我希望您能帮助我弄清楚发生了什么。

下面的代码与微调器代码不同。

这个想法是由FireStore文档ID填充spinner racePicker的数组,如下所示:

val db = FirebaseFirestore.getInstance()

val raceArray = ArrayList()
raceArray.add("Select race...")

db.collection("races").get().addOnSuccessListener { 
DocumentSnapshot ->
    for (document in DocumentSnapshot) {
        raceArray.add(document.id)
        Log.e("info", "raceArray contains values $raceArray")
    }

这是一个近似值。我可能已经将其设置为.toString(),或者可能在raceArray语句中使用了.addAll与.add。

(老实说,我不完全记得这是我的编码方式,但是它已经足够接近给出一个主意了,我现在正在从内存中键入内容。]

我的意图是像这样使用它:

racPicker.onSelectedItemListener = object : 
OnItemSelectedListener {
    override fun onNothingSelected() {
        }
    override fun onItemSelected(parent: AdapterView<*>?, view: 
View?, position: Int, id: Long) {
val selection = parent?.getItemAtPosition(position).toString()
    when (selection) {
        "$selection" -> raceArray.remove("Select race...").also{ 
statAllocator(selection) }
    }
}
}

出于某种原因,它会崩溃,但是如果我指定一个文字,例如“比赛名称”->“有趣”,“第二比赛名称”->“有趣”,等等,就可以使用。

最好使用

if (selectedItem.equals("$selection") {
        // do stufd
    }

代替?还是绝对需要将每个case / statement作为文字字符串来调用?我本质上是在寻找一种方法,使微调框的选定项(这是从FireStore数据库生成的文档名称的数组),然后“检查自身”并触发其他功能。

firebase dictionary kotlin google-cloud-firestore spinner
1个回答
0
投票

我通过第二个建议解决了这个问题。致歉。

© www.soinside.com 2019 - 2024. All rights reserved.