15

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);

                }
            });
4

8 回答 8

51

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

            }
        });
于 2013-02-25T10:16:30.663 回答
6

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

幸运的是,我们可以将点击监听器放在适配器上:

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);
于 2015-01-31T14:24:20.553 回答
5

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

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

于 2013-02-25T10:17:01.570 回答
4

我可能会偏离路线,但建议您使用OnItemSelectedListenerOnItemClickListenerSpinners 更广泛的方法:

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
    }
});
于 2013-02-25T10:15:13.803 回答
1

使用 getItemAtPosition:

supplier.getItemAtPosition(position).toString();
于 2013-02-25T10:14:28.437 回答
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

        }
    });
于 2013-02-25T10:15:34.113 回答
1

我遇到了这个问题。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) {

        }
    });
于 2016-06-13T10:59:43.090 回答
0

我认为supplier在您的代码中spinner,请替换此行

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

使用以下行:

 String msupplier= supplier.getItemAtPosition(position).toString(); 
于 2013-02-25T10:12:52.820 回答