Get the position of a spinner in Android
Asked Answered
Z

4

44

I'm trying to get the position (number) of the spinner when selected to use it in another Activity that will display a different map each time depending on the item selected. when I run the application it crashes. this is the first Activity code:

public class TestProjectActivity extends Activity {
    public Spinner spinner1;
    public Integer number;
    private Button valideButton;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MySpinner();
        valide_button();

    }

    public void MySpinner() {

        final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.num, android.R.layout.simple_spinner_item);
                                      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter);

        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parentView,
                    View selectedItemView, int position, long id) {
                // Object item = parentView.getItemAtPosition(position);

                TestProjectActivity.this.number = spinner1
                        .getSelectedItemPosition() + 1;

            }

            public void onNothingSelected(AdapterView<?> arg0) {// do nothing
            }

        });

    }

    public void valide_button() {

        valideButton = (Button) findViewById(R.id.valide_button);
        valideButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent intent = new Intent(TestProjectActivity.this,
                        MetroMapActivity.class);
                startActivity(intent);
            }

        });

    }

}
Zuzana answered 21/12, 2011 at 23:7 Comment(0)
R
113

The way to get the selection of the spinner is:

  spinner1.getSelectedItemPosition();

Documentation reference: http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()

However, in your code, the one place you are referencing it is within your setOnItemSelectedListener(). It is not necessary to poll the spinner, because the onItemSelected method gets passed the position as the "position" variable.

So you could change that line to:

TestProjectActivity.this.number = position + 1;

If that does not fix the problem, please post the error message generated when your app crashes.

Rodrique answered 21/12, 2011 at 23:11 Comment(3)
Already in the code, but when I try to use 'number' in another activity, it crashesZuzana
Try moving "number" into an application class. Activity variables are better off staying within the activity. Alternately, try setting number as a static member. read here: https://mcmap.net/q/193579/-using-the-android-application-class-to-persist-data/602661Rodrique
Briliant! I created an application class that will hold the data I will share between the 2 activities. Cheers man.Zuzana
K
3
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt = findViewById(R.id.button);
        spinner = findViewById(R.id.sp_item);
        setInfo();
        spinnerAdapter = new SpinnerAdapter(this, arrayList);
        spinner.setAdapter(spinnerAdapter);



        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //first,  we have to retrieve the item position as a string
                // then, we can change string value into integer
                String item_position = String.valueOf(position);

                int positonInt = Integer.valueOf(item_position);

                Toast.makeText(MainActivity.this, "value is "+ positonInt, Toast.LENGTH_SHORT).show();
            }

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

            }
        });







note: the position of items is counted from 0.
Kinglet answered 18/11, 2019 at 10:36 Comment(0)
B
1
    final int[] positions=new int[2]; 
    Spinner sp=findViewByID(R.id.spinner);

    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText( arg2....);
        }

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

        }
    });
Bawdry answered 22/12, 2011 at 10:33 Comment(0)
B
1
if (position ==0) {
    if (rYes.isChecked()) {
        Toast.makeText(SportActivity.this, "yes ur answer is right", Toast.LENGTH_LONG).show();
    } else if (rNo.isChecked()) {
        Toast.makeText(SportActivity.this, "no.ur answer is wrong", Toast.LENGTH_LONG).show();
    }
}

This code is supposed to select both check boxes.
Is there a problem with it?

Bankroll answered 6/1, 2015 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.