Undesired onItemSelected calls

后端 未结 5 1011
半阙折子戏
半阙折子戏 2020-12-02 19:04

I have 36 spinners that I have initialized with some values. I have used onItemSelectedListener with them. As usual, the user can interact with these spinners, firing the on

5条回答
  •  猫巷女王i
    2020-12-02 19:57

    When Spinner.setSelection(position) is used, it always activates setOnItemSelectedListener()

    To avoid firing the code twice I use this solution:

         private Boolean mIsSpinnerFirstCall = true;
    
        ...
        Spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                //If a new value is selected (avoid activating on setSelection())
                if(!mIsSpinnerFirstCall) {
                    // Your code goes gere
                }
                mIsSpinnerFirstCall = false;
            }
    
            public void onNothingSelected(AdapterView arg0) {
            }
        });
    

提交回复
热议问题