1

I have a spinner which is populated from the database. So if the spinner is clicked with no items in it,a spinner with an empty list item is shown.I want to avoid showing the spinner when it is empty and show a toast message. I have the logic to check the count of the items and show the toast message, but I cannot find out where to call the function.What is the event called when clicking the spinner and loading the spinner items?

viks
  • 404
  • 2
  • 6
  • 14

3 Answers3

2

Why not set an onClickListener?

Spinner s = (Spinner)findViewById(R.id.myspinner);
s.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        checkForItemCount();    //where you show your Toast if it's empty.
    }
});

Edit: I think I do not completely cover your question, so I tried some code myself. This works entirely for you.

final Spinner s = (Spinner)findViewById(R.id.spinner1); // get reference
    ArrayList<String> mList = new ArrayList<String>();  //some list without items
    s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mList));
    s.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP && mList.size() == 0) {
                toast("Empty"); //lazy method for toasting
                s.setPressed(false); //closes the spinner.
            }
            return true;
        }
    });
stealthjong
  • 10,858
  • 13
  • 45
  • 84
1

Spinner is a subclass of View, so, View.setOnClickListener() can be used to change onClick() implementation which will be called when it is clicked. You can override onClick() to check if the Spinner is empty and then show the Toast.

Correction:

Since you cannot setOnClickListener for Spinner, you can use the solution suggested in this question (the contents of which I've pasted below).

Instead of setting the spinner's OnClickListener, we are setting OnTouchListener and OnKeyListener.

spinner.setOnTouchListener(Spinner_OnTouch);
spinner.setOnKeyListener(Spinner_OnKey);

and the listeners:

private View.OnTouchListener Spinner_OnTouch = new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            doWhatYouWantHere();
        }
        return true;
    }
};
private static View.OnKeyListener Spinner_OnKey = new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
            doWhatYouWantHere();
            return true;
        } else {
            return false;
        }
    }
};
Community
  • 1
  • 1
Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47
  • The error message shown is 'Don't call setOnClickListener on AdapterView.You probably may want to use setOnItemClickListener.'.But this is not suitable for my purpose. – viks Dec 05 '12 at 14:49
0

This is the function when an item is selected if that's what you were asking. For more help you will need to post your code. Otherwise, we have no idea what you have going on

spinner.setOnItemSelectedListener(new OnItemSelectedListener()

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • No,I want to avoid showing the spinner items if the count is 0. I have to check when the user clicks on the textbox like view shown in the place of spinner – viks Dec 05 '12 at 14:31