用于微调器无效的Android Kotlin onItemSelectedListener

时间:2017-09-27 11:57:54

标签: android kotlin spinner

我有一个带有一些项目(字符串)的微调器。 我想将所选项目添加到列表中。我在网上看到我应该使用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<*>) {

        }
    }}}

由于

7 个答案:

答案 0 :(得分:44)

(在Kotlin中)使用此代码:

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

        }

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

        }

    }

答案 1 :(得分:2)

字符串将无法使用&#39; ==&#39;进行检查相反,你需要使用equals(&#34; string&#34;)

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

答案 2 :(得分:1)

谢谢,这对我有所帮助,它的工作正常! daysSpinner.onItemSelectedListener =对象:AdapterView.OnItemSelectedListener {             重写ononthingSelected(parent:AdapterView <*>?){

        }

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

        }

    }

答案 3 :(得分:0)

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

查看我的答案以获取更多信息。它会对您有所帮助:Android Koltin pass spinner values to mutable list

答案 4 :(得分:0)

代替:

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

尝试

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

当我尝试获取sqlite表的$ _ID并使用它们填充微调器时,情况也一样

答案 5 :(得分:0)

添加扩展功能

fun Spinner.selected(action: () -> 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 {
    // todo something
}

答案 6 :(得分:0)

// 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")
}