setOnItemClickListener不适用于Android Spinner

时间:2013-02-25 10:09:22

标签: android spinner

setOnItemClickListener不适用于android Spinner。这是我的代码:

supplier.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {
                    // TODO Auto-generated method stub

                    String msupplier=supplier.getSelectedItem().toString();
                    Log.e("Selected item : ",msupplier);

                }
            });

8 个答案:

答案 0 :(得分:46)

setOnItemClickListener不适用于android spinner。或者,尝试使用此方法:

  

setOnItemSelectedListener

更改您的代码:

supplier.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                String msupplier=supplier.getSelectedItem().toString();

                Log.e("Selected item : ",msupplier);
}

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

            }
        });

答案 1 :(得分:5)

1。我认为您应该检查包含微调器的xml文件。确保没有设置android:clickable = "false"

2。请检查您的微调器适配器,并确保在getView(int position, View convertView, ViewGroup parent)方法中,您没有设置任何小部件点击回拨事件,例如Button,{{1 }},Imageview

答案 2 :(得分:4)

OnItemSelectedListener()与点击侦听器的用途不同。最重要的是,如果再次单击当前选定的项目,它不会触发。在某些情况下,这可能不是问题,但在其他情况下,它可能是。所以真正的答案不是告诉人们使用不同的,可能不合适的听众,而是要解决原始问题。 : - )

幸运的是,我们可以将click侦听器放在适配器上:

public class SpinnerClickAdapter extends BaseAdapter implements SpinnerAdapter {
  protected OnItemClickListener listener;

  public SpinnerClickAdapter(Context context, ..., OnItemClickListener listener) {
    super();

    this.listener = listener;
  }

  @Override
  public View getDropDownView(final int position, View convertView, final ViewGroup parent) {
    if (convertView == null)
      convertView = LayoutInflater.from(context).inflate(R.layout.yourlayout, false);

    convertView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (listener != null)
          listener.onItemClick((AdapterView<?>) parent, v, position, getItemId(position));
      }
    });


    // ...

    return convertView;
  }
}

然后我们可以像往常一样使用它,唯一的区别是没有在微调器上设置它而是在适配器上:

Spinner spinner = (Spinner) findViewById(R.id.yourspinner);
SpinnerClickAdapter spinnerAdapter = new SpinnerClickAdapter(context, ..., new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // TODO
  }
});
spinner.setAdapter(spinnerAdapter);

答案 3 :(得分:3)

我可能会偏离正轨,但考虑建议您使用比OnItemSelectedListener更宽的OnItemClickListener来使用Spinners:

supplier.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
        //do something here
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        //optionally do something here
    }
});

答案 4 :(得分:1)

使用getItemAtPosition:

supplier.getItemAtPosition(position).toString();

答案 5 :(得分:1)

使用此

supplier.setOnItemSelectedListener(new OnItemSelectedListener() {

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

        }

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

        }
    });

答案 6 :(得分:0)

我认为代码中的supplier表示spinner,请替换此行

 String msupplier=supplier.getSelectedItem().toString();

以下行:

 String msupplier= supplier.getItemAtPosition(position).toString(); 

答案 7 :(得分:0)

我遇到过这个问题。 setOnItemClickListener不适用于微调器。我的解决方案如下:

mSpinnerYears.setAdapter(adapter);
    mSpinnerYears.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Logger.d(new StringBuilder(2).append("TAG:").append(TAG).toString(),
                    new StringBuilder(3).append("click pos = ").append(position).append(
                            mSpinnerYears.getItemAtPosition(position).toString()).toString());
        }

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

        }
    });