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

后端 未结 8 1791
抹茶落季
抹茶落季 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:38

    Simple and easy is this... validate with a boolean to see if is first time...

    Spinner mySpinner = (Spinner)findViewById(R.id.spinner_xml_pro);
            mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    if(isSpinnerInitial){ // globar var boolean isSpinnerInitial = false;
                     //do something
                   }else
                        isSpinnerInitial=true;
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
    
                }
            });
    

    Check this with spinner.post(new Runnable()...) or this other my source

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-12-18 20:41

    We can use a flag, and just enable it when the spinner is really touched.

    private boolean isSpinnerTouched = false; 
    
    spinner.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    isSpinnerTouched = true;
                    return false;
                }
            });
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapter, View arg1,
                            int arg2, long arg3) {
                        if (!isSpinnerTouched) return;
                        // do what you want 
                        }
            });
    
    0 讨论(0)
  • 2020-12-18 20:51

    I have found a solution for this problem and posted it here (with code sample):

    Spinner onItemSelected() executes when it is not suppose to

    0 讨论(0)
  • 2020-12-18 20:52

    There are no any way to avoid this.

    You may add some flag, indicating readiness of your application and use it in your onItemSelected() method to decide, what to do in each case.

    0 讨论(0)
  • 2020-12-18 21:00

    I have solved this issue, You can avoid this issue by not setting any default values to the spinner

            int initialposition=spinner.getSelectedItemPosition();
            spinner.setSelection(initialposition, false);
    

    This will avoid entering into onItemSelected()

    0 讨论(0)
提交回复
热议问题