Android Spinner: Get the Selected Item Change Event

Android Spinner: Get the selected item change event

Some of the previous answers are not correct. They work for other widgets and views, but the documentation for the Spinner widget clearly states:

A spinner does not support item click
events. Calling this method will raise
an exception.

Better use OnItemSelectedListener() instead:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}

});

This works for me.

Note that onItemSelected method is also invoked when the view is being build, so you can consider putting it inside onCreate() method call.

spinner selected item change event triggers in initialization

I would expected the solution above to work but if it doesn't a simple boolean flag would allow you to detect when the items are loaded.

1.Create a boolean isDataLoaded = false;

2.Add a condition in your onItemSelected listener

if (isDataLoaded) { //sort categories };

3.When the data loaded just set the boolean to true;

Changing Spinner list content after item is selected Android

your code is a dead loop. and if the adapter item data changed,
such as add, remove, clear by adapter you can use notifyDataSetChanged
method.

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

adapter.clear()/add(object)/addAll();
adapter.notifyDataSetChanged();

then the view will update but not init.

if the whole data changed, you can also recreate an adapter, and then set data

dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, newStringList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCategory.setAdapter(dataAdapter)

At the same time, you can also clear and addAll, then notifyDataSetChanged

adapter.clear() //remove all data;
adapter.addAll(source);
adapter.notifyDataSetChanged();

//or remove it, then insert it in the first. that is acturl for your conditon
adapter.remove(objcet) //remove special data;
adapter.insert(object,index);
adapter.notifyDataSetChanged();

Secondly, you can also set a Tag to make when init the onItemSelected not called. the code like this:

//define a tag when first it is 0
int check = 0;
//then in the onItemSelected add a check condition
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
if(++check> 1) {
//do some things when item selected.
}
}

use notifyDataSetChanged to update data, not every time init spinner. if you only want to remove the init selected effect, use the check tag.

Spinner already Selected Item Selection event

To call an event on the already selected item in Spinner You can do it in this way:

  1. Create Your own Spinner class by extending AppCompatSpinner:
import android.content.Context
import android.util.AttributeSet
import android.util.Log

class MySpinner(context: Context, attrs: AttributeSet?) : androidx.appcompat.widget.AppCompatSpinner(
context,
attrs
)
{
var listener: OnItemSelectedListener? = null

override fun setSelection(position: Int)
{
super.setSelection(position)
if (position == selectedItemPosition)
{
listener!!.onItemSelected(this, selectedView, position, selectedItemId)
}
}

override fun setOnItemSelectedListener(listener: OnItemSelectedListener?)
{
this.listener = listener
}
}

  1. Use it in Your layout:
<com.yourcompany.kotlintest.MySpinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

  1. When creating a layout do this:
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val spinner = findViewById<MySpinner>(R.id.spinner)

spinner.adapter = ArrayAdapter(
this,
android.R.layout.simple_spinner_dropdown_item,
arrayListOf("One", "Two", "Three")
)

spinner.onItemSelectedListener = object :
AdapterView.OnItemSelectedListener
{
override fun onItemSelected(
parent: AdapterView<*>,
view: View, position: Int, id: Long
)
{
Log.d("MyTag", "Click item at pos $position")
}

override fun onNothingSelected(parent: AdapterView<*>)
{
Log.d("MyTag", "Nothing selected")
}
}
}
}

Now when You reselect the same item function will be executed

Spinner on value Change

From the Hello Spinner tutorial:

Now create a nested class that implements AdapterView.OnItemSelectedListener. This will provide a callback method that will notify your application when an item has been selected from the Spinner. Here's what this class should look like:

public class MyOnItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}

public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}

The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. The former is called when an item from the AdapterView is selected, in which case, a short Toast message displays the selected text; and the latter is called when a selection disappears from the AdapterView, which doesn't happen in this case, so it's ignored.
Now the MyOnItemSelectedListener needs to be applied to the Spinner. Go back to the onCreate() method and add the following line to the end:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

In other words, you need to create an OnItemSelectedListener that modifies the value of the second spinner, and attach it to the first spinner.

Get spinner selected items text?

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();

How to set selected item for Spinner in Android

You can do similar to the following. I am just giving you an idea here.

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.setSelection(position + 1);
}

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

}
});

How to get selected item from spinner in android

Try this!!!

spin.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String yourName=spiner.getSelectedItem().toString();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});

If code above not help you. Try this!!

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();


Related Topics



Leave a reply



Submit