How to Get the Selected Value of a Spinner

How to get Spinner selected item value to string?

Try this:

String text = mySpinner.getSelectedItem().toString();

Like this you can get value for different Spinners.

How do you get the selected value of a Spinner?

To get the selected value of a spinner you can follow this example.

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.

Within "onItemSelected" method of that class, you can get the selected item:

public class YourItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
}

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

Finally, your ItemSelectedListener needs to be registered in the Spinner:

spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

Android - How to get the selected item value from a spinner and put it into a string?

Use:

imc_met=Spinner.getSelectedItem().toString();

Instead:

imc_met = parent.getItemAtPosition(pos).toString();

Updated:

Seem you assigning Listener to your spinner not in correct way, do something like below:

spin.setOnItemSelectedListener(new OnItemSelectedListener() {

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

}

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

}
});

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();

Get spinner selected items text?

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

How to get Spinner value?

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

How to get spinner value after item select in spinner

If you don't want to use a custom adapter then fetch selected spinner position and using that position fetch your State model

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// int position = spinnerState.getSelectedItemPosition();

if(position >= 1){
AllStates allStates = stateList.get(position - 1)
}
}

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

}
});

Now you can access id from allStates model

How to set selected item of Spinner by value, not by position?

Suppose your Spinner is named mSpinner, and it contains as one of its choices: "some value".

To find and compare the position of "some value" in the Spinner use this:

String compareValue = "some value";
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.select_state, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
if (compareValue != null) {
int spinnerPosition = adapter.getPosition(compareValue);
mSpinner.setSelection(spinnerPosition);
}

How to get the value of a selected item in a spinner?

First of all, you need to implement the interface which you want to use the methods. In your case, you could do it like this:

public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{

Then, put @Override upon the methods to describe the behaviors you want:

  @Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// An item was selected. You can retrieve the selected item using
parent.getItemAtPosition(pos);

}

   @Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback

}

And then set your spinner listener inside your onCreate():

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactSpinner = (Spinner) findViewById(R.id.contact_list);
contactSpinner.setOnItemSelectedListener(this);
....


Related Topics



Leave a reply



Submit