drew moore
drew moore

Reputation: 32670

Getting an onItemSelectedListener to fire when item is already "selected"

I have two spinners. When you select an item in one of them, the item selected (now appearing at the top), turns red. The spinners are tied to one another in a mutually-exclusive sense: If you select something in one, the other one goes back to the initial ("title") selection and turns white.

This is all done via the onItemSelected listeners:

    sectionSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1,
                int position, long arg3) {
            System.out.println("SECTION SPIN # " + position);               

            issueSpin.setSelection(0);                                      
            ((TextView) issueSpin.getChildAt(0)).setTextColor(Color.parseColor("#FFFFFF"));  
            ((TextView) arg1).setTextColor(Color.parseColor("#E3170D"));
....

and vice versa for the "issue spinner". My problem is that, if I'm going from one spinner to the other and I select the top item, the onItemSelectedListener doesn't register (the println statement (or, rather, lack thereof, proves this to me) because the item being selected is already selected. So nothing happens.

What I need is something equivalent to a ButtonGroup in Swing, where I can have two seemingly different menus but add all the children to a single "mutually-exclusive" buttongroup so that only one can be selected at a time. Is there any mechanism in android that will give me something like this, or can anyone help me with a workaround?

Upvotes: 0

Views: 601

Answers (1)

David Manpearl
David Manpearl

Reputation: 12656

It is impossible for a Spinner to trigger its onItemSelected() callback for an item that is already selected.

There is are examples of overriding the Spinner class that I have not been able to get to work. There is even an example on this site of overriding some of the AbsSpinner functions to make the Spinner trigger items that are already selected, but unfortunately that example no longer works due to API changes.

Therefore, I believe the best way to do this is to use a combination of a TextView, a Button (for the spindown), and an AlertDialog to hold the same ArrayAdapter that you already send to the Spinner.

You will have to do custom work for the TextView/Button layout as well as more custom work to allow changing color/text properties of the list items, but at least your DialogInterface.onItemClickedListener() will always trigger.

Upvotes: 1

Related Questions