setOnItemClickListener Not Works with Spinner

2019-01-25 07:32发布

setOnItemClickListener Not Works with Spinner. Here is my code...

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条回答
祖国的老花朵
2楼-- · 2019-01-25 08:14

1. I think you should check your xml file which contains the spinner. Make sure haven't set android:clickable = "false".

2. please check your spinner adapter and make sure in your getView(int position, View convertView, ViewGroup parent) method, you haven't set any widget click callback event such as Button,Imageview,TextView

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-25 08:14

i have faced this issues. setOnItemClickListener will not works with spinner. My solution is as following:

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

        }
    });
查看更多
干净又极端
4楼-- · 2019-01-25 08:15

Use getItemAtPosition:

supplier.getItemAtPosition(position).toString();
查看更多
放荡不羁爱自由
5楼-- · 2019-01-25 08:17

setOnItemClickListener will not works with spinner. Try using this method:

setOnItemSelectedListener

Change your code like following:

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

            }
        });
查看更多
三岁会撩人
6楼-- · 2019-01-25 08:19

Use this

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

        }
    });
查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-01-25 08:21

OnItemSelectedListener() serves a different purpose than a click listener. Most importantly, it doesn't fire if you click on the currently selected item again. In some cases this might not be a problem but in other cases, it might well be. So the real answer is not to tell people to use a different and probably unsuitable listener but to solve the original problem. :-)

Fortunately, we can place the click listener on the adapter instead:

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

and then we can use it as usual, with the only difference of not setting it on the spinner but on the adapter instead:

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);
查看更多
登录 后发表回答