1

Having an odd issue here and don't know why it doesn't work, I'm not that used to java yet. to determine the selected item what needs to be done? the spinner has 8 items and 'position' never = 1, or any other number. onItemSelected() is definitely getting fired so is the if statement wrong?

public void onItemSelected(AdapterView parent, View v,int position, long id) {

    if (position == 1) //do something
}

EDIT: thanks Lion it turns out position doesnt seem to do anything. however, this works.

String s = parent.getSelectedItem().toString();
if (s == "1")//do something
Bob Nodger
  • 11
  • 4
  • 1
    "*I'm not that used to java yet*", really? then you need to spend some time to it before you start with Android projects. – Lion Jul 08 '12 at 18:38
  • thanks for the help lol. you see something wrong or not? you mind pointing it out? – Bob Nodger Jul 08 '12 at 18:47
  • I don't know the rest of your code. Therefore, I can't answer in its entirely but you may refer to these questions. [How do you get the selected value of a spinner — Android](http://stackoverflow.com/questions/2652414/how-do-you-get-the-selected-value-of-a-spinner-android), [Get spinner selected items text?](http://stackoverflow.com/questions/5787809/get-spinner-selected-items-text), [Android Spinner: Get the selected item change event](http://stackoverflow.com/questions/1337424/android-spinner-get-the-selected-item-change-event). Hope these questions and answers help you! – Lion Jul 08 '12 at 18:58

1 Answers1

0

You have to implement the proper listener for the spinner.

yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 

      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              if (position == 1){
                 //Do something}
              }
      }

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

If you're "not that used to Java" I recommend you read some basics.

yugidroid
  • 6,640
  • 2
  • 30
  • 45
  • thanks, i can manage that much, i dont dabble in java often but spend my life programming in other languages. – Bob Nodger Jul 08 '12 at 19:11
  • You're welcome. If you know other OO languages then Java would be piece of cake for you ;) Accept my answer if it works for you, please. – yugidroid Jul 08 '12 at 19:16