Spinner item gets automatically selected upon entering activity. How do I avoid this?

后端 未结 8 1816
抹茶落季
抹茶落季 2020-12-18 20:04

I have a spinner in my Android app, and its onItemSelected() event automatically gets triggered upon entering the activity.

How do I avoid this?

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-18 20:40

    You can avoid it by ignoring the first click by,

    private boolean isSpinnerInitial = true; //As global variable
    
    public void onItemSelected(xxx xxx, xxx xxx, xxx xxx, xxx xxx) {
        if(isSpinnerInitial) {
            isSpinnerInitial = false;
            return;
        }
        // Write your code here
    }
    

提交回复
热议问题