Cannot set OnItemClickListener for spinner in andr

2019-04-09 00:53发布

问题:

In a customized RecyclerView.Adapter<CustomAdapter.ViewHolder>, I set an adapter for a spinner and found that the value of my spinner isn't change after selection, so I tried to add an OnItemClickListener on my spinner:

override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {

    // The selectable items of the spinner is dynamically generated from getItems() function
    val selectableItems: List<String> = getItems()

    val spinnerAdapter = ArrayAdapter<String>(this.context, androi.R.layout.simple_spinner_item)
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    spinnerAdapter.addAll(selectableItems)

    viewHolder.mySpinner.adapter = spinnerAdapter

    // the error occurs on the following line:
    viewHolder.mySpinner.onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, i, l ->
        adapterView.setSelection(i)
    }

    viewHolder.mySpinner.setSelection(origionSelectedPosition)
}

I didn't get any warning or error while compiling, however during run time, I got a RuntimeException said that setOnItemClickListener cannot be used with a spinner. It is kind of strange. Did I do anything wrong in my code? Or does anyone know why my spinner didn't change with the corresponding selection?

Further description for Kotlin solution:

I had ever tried to use onItemSelectedListener but failed to find a suitable way to use it in kotlin. But finally! The following code solved my problem:

    viewHolder.mySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

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

        }

        override fun onNothingSelected(parent: AdapterView<out Adapter>?) {

        }

    }

回答1:

Use setOnItemSelectedListener instead of onItemClickListener.

viewHolder.mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});


回答2:

Following sasikumar's answer, I solved it in kotlin with the following code:

viewHolder.mySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

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

    }

    override fun onNothingSelected(parent: AdapterView<out Adapter>?) {

    }

}


回答3:

First initialize spinner at onCreate function.

 var spinner : Spinner ?= null

spinner = findViewById(R.id.spinner) as Spinner 

Now add details with Spinner. Here i have used array from string resource.

 fun addSpinnerData(){

   var arr = (resources.getStringArray(R.array.data_list).toMutableList());
    adapter = ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            arr)

    spinner!!.setAdapter(adapter);

    spinner!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
        override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
            Toast.makeText(applicationContext," On Click",Toast.LENGTH_SHORT).show();
        }

        override fun onNothingSelected(p0: AdapterView<*>?) {
        }
    }

}

By this way you can achieve onItemSelectedListener for Spinner



回答4:

You can create a very simple extension function:

file: Extensions.kt

fun Spinner.setOnItemSelectedListener(listener: AdapterView.OnItemSelectedListener) {
    onItemSelectedListener = listener
}

Then you can call it the way you want

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

    }

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

    }
})

Similar to previous answers, but it looks more like the original version instead of the assignment