Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Synchronizing" two spinners

Tags:

android

I have two spinners that I want to be "tied" to one another in a mutually-exclusive sense: If you select an item in one, that item's text turns red and appears at the top, while the other one goes back to displaying the initial ("title") selection (if another item was previously selected), and its text 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) {              
            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 because the item being selected is already selected.

I've been told that this is not possible. Or, rather, I've been told that it is impossible for an onItemSelected listener to fire on an item that is already selected. While I realize this is technically true, this problem seems relatively simple and I'm sure there must be a workaround to produce the desired effect.

I have a few questions regarding some that I'm pondering:

  1. Is there any way to set all items in a spinner as unselected, while still displaying one of them?
  2. Could I utilize a different type of event (i.e. 'setOnTouchListener', 'setOnClickListener', etc), presumably on the top item, in conjunction with the onItemSelectedListener?
  3. Should I utilize a different type of event by itself, perhaps on the Views inflated in the spinner themselves, without the onItemSelectedListener?
  4. Can you help me find a better strategy than those alluded to in the bulletpoints above?
like image 244
drew moore Avatar asked Apr 02 '13 04:04

drew moore


2 Answers

Instead of using a Spinner, use a Button that visually resembles a non-active Spinner. When the button is clicked, show a custom Dialog containing a ListView. You can use the setSelection method and/or the position in the Adapter's getView to show which item is currently selected. The ListView's OnItemClickListener will get notified that something got clicked regardless of whether the item was selected or not.

When you find which item was clicked, hide the Dialog and notify the button-containing Activity of this, so that it can update both Buttons if needed.

I have implemented something really similar to this using DialogFragments and a DialogFragmentParent and it works fine.

like image 104
Shade Avatar answered Nov 03 '22 12:11

Shade


Have you consider something like below,

sectionSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View arg1,
            int position, long arg3) {
        updateView("sectionSpin");
     }
}

And for other one

    issueSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View arg1,
            int position, long arg3) {
        updateView("issueSpin");
     }
} 

and now

private void updateView(String spinner){
    if (spinner.equals(sectionSpin)){
        //update spinner per requirement

    }
    else{
    //update spinner per requirement
    }

}

You can also consider some additional variable or parameters to keep track if the item selected for first time or not.

like image 35
minhaz Avatar answered Nov 03 '22 13:11

minhaz