setOnItemClickListener不与Android微调工作(setOnItemClick

2019-07-20 11:28发布

setOnItemClickListener不与Android微调工作。 这里是我的代码:

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

                }
            });

Answer 1:

setOnItemClickListener不会与Android spinner.Alternatively工作,请尝试使用这种方法:

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

            }
        });


Answer 2:

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

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



Answer 3:

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


Answer 4:

我可能要偏离航线,但认为建议您使用OnItemSelectedListener比更广泛的OnItemClickListener的纱厂:

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


Answer 5:

使用getItemAtPosition:

supplier.getItemAtPosition(position).toString();


Answer 6:

用这个

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

        }
    });


Answer 7:

我认为supplier在你的代码意味着spinner ,不要替换该行

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

与下面行:

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


Answer 8:

我面临这个问题。 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) {

        }
    });


文章来源: setOnItemClickListener doesn't work with Android Spinner