【问题标题】:Android Kotlin onItemSelectedListener for spinner not working用于微调器的 Android Kotlin onItemSelectedListener 不起作用
【发布时间】:2023-03-23 01:37:01
【问题描述】:

我有一个带有一些项目(字符串)的微调器。 我想将所选项目添加到列表中。我在网上看到我应该使用onItemSelectedListener而不是onItemClickListener

我实现了这个,但我不知道如何完成将其添加到列表的步骤。

class NewKitListActivity : AppCompatActivity() {

var spinnerArray = arrayOf("Dumbell", "Punching Bag", "Yoga Ball", "Skipping Rope")
//var spinnerArray = arrayOf(DataService.kitList)



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



    val spinner = newKitItemSpinner
    val spinnerArrayAdapter = ArrayAdapter<String>(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

    spinner.onItemSelectedListener = object : OnItemSelectedListener {
        override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
            val selectedItem = parent.getItemAtPosition(position).toString()
            if (selectedItem == "Add new category") {
                // do your stuff
            }
        } // to close the onItemSelected

        override fun onNothingSelected(parent: AdapterView<*>) {

        }
    }}}

谢谢

【问题讨论】:

  • 创建一个新的 MutableList 并将您的项目添加到此列表中。
  • @just,你能写一个我如何将它传递给可变列表的例子吗?我会很感激的!

标签: android kotlin spinner


【解决方案1】:

(在 Kotlin 中)使用此代码:

yourSpinner?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
        override fun onNothingSelected(parent: AdapterView<*>?) {

        }

        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

        }

    }

【讨论】:

    【解决方案2】:

    谢谢这对我有帮助,它工作正常!

    daysSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                override fun onNothingSelected(parent: AdapterView<*>?) {
    
                }
    
                override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
    
                }
    
            }
    

    【讨论】:

    • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation 将通过展示为什么这是解决问题的好方法,并使其对有其他类似问题的未来读者更有用,从而大大提高其长期价值。请edit您的回答添加一些解释,包括您所做的假设。
    • 该代码对于从 Spinner 中选择一项来获取 Int 很有用,如果没有选择任何内容,它将获得 0,因为 Spinner 从零位置开始。
    【解决方案3】:

    string 将无法使用 '==' 进行检查,您需要使用 equals("string")

    if (selectedItem.equals("Add new category")) {
                // do your stuff
            }
    

    【讨论】:

    • 在 Kotlin 中比较 Strings 时应该使用 ==
    • 只在 java 不是 kotlin
    【解决方案4】:

    添加扩展功能

    fun Spinner.selected(action: (position:Int) -> Unit) {
        this.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {}
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                action()
            }
        }
    }
    

    简单使用

    spinner.selected {
        println(it) //selected position
    }
    

    【讨论】:

    • 谢谢.. 我们如何在选中位置获得位置{ }
    • @HarmanKhera 更新!加油!)
    【解决方案5】:

    我是这样实现的。 1.创建空的可变列表 2.在微调器上设置onItemSelectedListner 3.当用户选择项目时,将其添加到可变列表中

    查看我的这个答案以获取更多信息。它会帮助你:Android Koltin pass spinner values to mutable list

    【讨论】:

      【解决方案6】:

      代替:

      var spinnerArray = arrayOf("Dumbell", "Punching Bag", "Yoga Ball", "Skipping Rope")
      

      试试

      var spinnerArray = mutableListOf<String>("Dumbell", "Punching Bag", "Yoga Ball", "Skipping Rope")
      

      当我尝试获取 sqlite 表的 $_ID 并用它们填充微调器时遇到了同样的情况

      【讨论】:

        【解决方案7】:
        // Extends AdapterView.OnItemSelectedListener
        class Dialogs : DialogFragment(), AdapterView.OnItemSelectedListener {}
        
        //Somewhre in onCreate (I'm using databinding but you don't have to)
         binding.spinnerDialogEstados.onItemSelectedListener = this
        
        // then implement members...
        
              override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            TODO("Not yet implemented")
        }
        
        override fun onNothingSelected(parent: AdapterView<*>?) {
            TODO("Not yet implemented")
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-18
          相关资源
          最近更新 更多