10

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.

Gimmy88
  • 295
  • 2
  • 5
  • 13

6 Answers6

34

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

            }
        });
RobinHood
  • 10,897
  • 4
  • 48
  • 97
  • 1
    If i put it into OnItemSeelected i give me the error: "Cannot make a static reference to the non-static method getSelectedItem() from the type AdapterView" – Gimmy88 Mar 21 '13 at 11:18
  • Sure, simply i declare it in this way: "public String imc_met;" – Gimmy88 Mar 21 '13 at 11:24
  • cleaned and refreshed but nothing change – Gimmy88 Mar 21 '13 at 11:36
  • @RobinHood: i added the code into onCreate method, but it always tell me the error: "Cannot make a static reference to the non-static method getSelectedItem() from the type AdapterView" on the row "imc_met=Spinner.getSelectedItem().toString();" – Gimmy88 Mar 21 '13 at 11:43
  • At the end it seems to work.I added your code into OnCreate method, i used "imc_met = arg0.getItemAtPosition(arg2).toString();", and then i added these two methods in the public class: public void onItemSelected(AdapterView> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } public void onNothingSelected(AdapterView> arg0){ // TODO Auto-generated method stub } I have to check the code in order to understand how it works, but now it give me no error and the value of imc_met is correct. – Gimmy88 Mar 21 '13 at 13:10
  • can you please have a look at this too: http://stackoverflow.com/questions/40748389/appcompatspinners-entries-not-opening-timepickerdialog-on-selecting-it-second-t – Hammad Nasir Nov 23 '16 at 02:37
7

Try this:

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

I'm sorry. I forgot

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        imc_met=Spinner.getSelectedItem().toString();
        }
    }
Linga
  • 10,379
  • 10
  • 52
  • 104
  • I try but it tells me: "Cannot make a static reference to the non-static method getSelectedItem() from the type AdapterView" – Gimmy88 Mar 21 '13 at 11:11
  • 1
    It gives me the same erroe putting it after "spinner.setAdapter(adapter);" and by deleting onItemSelected method – Gimmy88 Mar 21 '13 at 11:16
  • Need of `onItemSelected` if spinner value change then it won't return update one because you are suggesting him to do within `onCreate`. – RobinHood Mar 21 '13 at 11:18
  • If i delete the onItemSelected method it tells me "The type MainActivity must implement the inherited abstract method AdapterView.OnItemSelectedListener.onItemSelected(AdapterView>, View, int, long)" on the row of "public class MainActivity" – Gimmy88 Mar 21 '13 at 11:22
  • if i put it into onCreate method it tells me: "The type new AdapterView.OnItemSelectedListener(){} must implement the inherited abstract method AdapterView.OnItemSelectedListener.onNothingSelected(AdapterView>)" – Gimmy88 Mar 21 '13 at 11:29
  • Already added, but it tells me "Cannot make a static reference to the non-static method getSelectedItem() from the type AdapterView" – Gimmy88 Mar 21 '13 at 11:32
2

is

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

not

imc_met=Spinner.getSelectedItem().toString();
Eternal1
  • 5,447
  • 3
  • 30
  • 45
Yoshi
  • 19
  • 6
1
int position = Arrays.asList(getResources().getStringArray(R.array.country_value_array)).indexOf(address.getCountry());

This will get index by the value.

SysDragon
  • 9,692
  • 15
  • 60
  • 89
0

If spinner isn't define in onCreate() method then have to use this:

String spinner_value = ((Spinner)findViewById(R.id.spinner1)).getSelectedItem().toString(); 
-2

Try this code in your onCreate() method:

spinner.setOnItemSelectedListener(this);
arkascha
  • 41,620
  • 7
  • 58
  • 90
Isras
  • 1