Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setOnItemClickListener doesn't work with Android Spinner

setOnItemClickListener doesn't work with android 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);

                }
            });
like image 320
Sonu Avatar asked Feb 25 '13 10:02

Sonu


2 Answers

setOnItemClickListener will not work with android spinner.Alternatively, try using this method:

setOnItemSelectedListener

Change your code like this:

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

            }
        });
like image 124
Ebin Sebastian Avatar answered Oct 07 '22 02:10

Ebin Sebastian


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);
like image 33
Gábor Avatar answered Oct 07 '22 03:10

Gábor