android spinner onselect item单击监听器无触发器

时间:2013-02-01 22:20:58

标签: android android-spinner

这就是我定义微调器的方法

s_province = (Spinner) findViewById(R.id.s_province);
        ArrayAdapter<String> provinceAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, Data.provinces);
        provinceAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s_province.setAdapter(provinceAdapter);
        s_province.setOnItemSelectedListener(this);

我的类是从OnItemSelectedListener实现的,我重写了这个方法

@Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        switch (arg1.getId()) {
        case R.id.s_province:
            Log.d("here", "there");
            break;

        default:
            break;
        }
    }

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

    }

但是onItemSelect没有被区分,为什么还请?

1 个答案:

答案 0 :(得分:2)

两件事:

  1. 如果您确实想检查您的方法是否有效,请在Logswitch案例之外添加default语句,以便您可以确定正在调用该方法。

  2. that represents position以来,您应该使用arg2。使您的开关与位置一起工作,而不是传递View。还要从Eclipse分配的默认名称重命名变量。 arg0,1,2, etc对您或其他任何查看您代码的人都没有帮助。

  3. 例如

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
      Toast.makeText(view.getContext(),"onItemSelected called", Toast.LENGTH_LONG).show();
      int spinnerId = parent.getId();
      if (spinnerId == R.id.s_province)
      {
        switch (position)
        {
          case 0: 
            //do something if first position was clicked
          break:
          case 1:
            //do something else
            break;
          default: 
            //if for any reason no position matches.
            break;
        }
      }
      else if (spinnerId == R.other_id_in_xml)
      {
        switch (position)
        {
          case 0: 
            //do something if first position was clicked
          break:
          case 1:
            //do something else
            break;
          default: 
            //if for any reason no position matches.
            break;
        }
      }
      //etc
    }