Android spinner position and if statements

China☆狼群 提交于 2020-01-24 23:54:27

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/11385623/android-spinner-position-and-if-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!