Get the position of a spinner in Android

前端 未结 4 1329
醉梦人生
醉梦人生 2020-12-14 06:07

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 selecte

4条回答
  •  -上瘾入骨i
    2020-12-14 06:24

    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.

提交回复
热议问题