Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

How do I avoid this?

like image 552
user594720 Avatar asked Mar 20 '11 11:03

user594720


1 Answers

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 
                    }
        });
like image 139
thanhbinh84 Avatar answered Nov 02 '22 19:11

thanhbinh84