14

I'm creating a spinner and I've added an OnItemSelectedListener to it. However I've noticed that it fires on create. Now I was wondering if there was a way to ignore/discard it.

I know I could use a boolean value, but that's a bit "dirty".

bofredo
  • 2,348
  • 6
  • 32
  • 51
user211992
  • 330
  • 6
  • 17

5 Answers5

2

Here is my solution.

I need to ignore the first item selection event because there is a dependency between the Route Grade Spinner and the Route Checkbox.

And all my controls are setup based on a previous visit to the activity.

// Used to count the number of times the onItemSelected gets fired
private int mGradeSelectionCount = 0;

private void attachHandlers() {
    OnItemSelectedListener gradeRangeSelectionMadeListener;
    gradeRangeSelectionMadeListener = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapter, View view, int position, long id) {
            // If the counter is 0 then we can assume that it is android firing the event
            if (mGradeSelectionCount++ < 1) {
                return;
            }
            if (mCmbGradeFrom.getSelectedItemPosition() == 0) {
                // Uncheck the Route checkbox
                mChkTypeRoute.setChecked(false);
            } else {
                // Check the Route checkbox
                mChkTypeRoute.setChecked(true);
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // Dont care, keep the same values as before

        }
    };
    mCmbGradeFrom.setOnItemSelectedListener(gradeRangeSelectionMadeListener);
    mChkTypeRoute.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!isChecked) {
                mCmbGradeFrom.setSelection(0);
                mCmbGradeTo.setSelection(0);
            }
        }
    });
}
concept
  • 761
  • 2
  • 10
  • 16
1

This may help you.

@Override  
public void onItemSelected( AdapterView<?> parent, View view, int position, long id)
{
    if(view!=null &&  view.getId()!=0){
        //do your code here to avoid callback twice 
    }
}
gunr2171
  • 16,104
  • 25
  • 61
  • 88
1

You should not attempt to prevent the call to the OnItemSelectedListener.

By default, Android Spinners select the first item returned by the Adapter, and therefore the OnItemSelectedListener is called to trigger some action on that item.

I would advise that the first item in your Spinner Adapter be a blank item, and your OnItemSelectedListener can ignore that blank item based on its id.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Shams Shafiq
  • 3,884
  • 3
  • 16
  • 21
0

Well I think I found nice solution for me, I had it in mind from start but... I have custom wrapper class based on android Handler , that is called DoLater, and also there is custom Adapter based on Listener so you cant copy paste this but you will get idea. Dangerous thing is just that somehow delay 500 can be to long and View can be already destroyed (when user do some wired stuff quickly or phone gets slow...) so DoLater cares of that so it is not called when activity is not resumed. But this way OnItemSelectedListener is not fired on create.

public void onResume() {
    super.onResume();
    new DoLater(this, 500) {
                public void run() {
                    new OnSpinnerSelectedAdapter(getBowSpinner()) {
                        protected void onItemSelected(int position) {
                            onBowSelected(position);
                        }
                    };
                }
            };
}
Renetik
  • 5,887
  • 1
  • 47
  • 66
0

If anyone else comes across this question, it may be worth having a look at a related question I asked a while ago, which has several answers with good ideas on how to work around this issue.

Community
  • 1
  • 1
Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72
  • I have another issue with the listener. if i fire an async task from the OnItemSelected method, and then rotate the getLastNonConfigurationInstance retuns null for the task i save at the onRetainNonConfigurationInstance. But if i put the code outside in the oncreate it will work well.... – Maxrunner Dec 21 '11 at 20:30
  • 3
    @cosmincalistru: Fixed the link - I just copied pasted what was in my browser URL at the time, didn't realise it was an answer instead of the question. Perhaps, in time, I'll come to forgive myself. – Amos M. Carpenter Sep 27 '12 at 00:17