i read many similar questions on this thread, but none of them help me... This is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.imc_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.imc_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// An item was selected. You can retrieve the selected item using
imc_met = parent.getItemAtPosition(pos).toString();
}
I declare imc_met as public String imc_met;
. The problem is that imc_met does not contain the value of the selected item of the spinner, but it's null...
Where's the problem?
Thx in advance.
Try this code in your
onCreate()
method:This will get index by the value.
Try this:
I'm sorry. I forgot
If spinner isn't define in onCreate() method then have to use this:
is
not
Use:
Instead:
Updated:
Seem you assigning Listener to your spinner not in correct way, do something like below: