Undesired onItemSelected calls

后端 未结 5 1010
半阙折子戏
半阙折子戏 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条回答
  •  半阙折子戏
    2020-12-02 19:55

    The way I solved this is by saving the OnItemSelectedListener first. Then set the OnItemSelectedListener of the Spinner to the null value. After setting the item in the Spinner by code, restore the OnItemSelectedListener again. This worked for me.

    See code below:

            // disable the onItemClickListener before changing the selection by code. Set it back again afterwards
            AdapterView.OnItemSelectedListener onItemSelectedListener = historyPeriodSpinner.getOnItemSelectedListener();
            historyPeriodSpinner.setOnItemSelectedListener(null);
            historyPeriodSpinner.setSelection(0);
            historyPeriodSpinner.setOnItemSelectedListener(onItemSelectedListener);
    

提交回复
热议问题