tags:

views:

37

answers:

1

i have code that runs onitemselectedlistener event of spinner.So when i am in the method :

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    //I want to do something here if its a user who changed the the selected item
}

... can i know whether item selection was changed programmatically or by a user action through UI.

A: 

I don't know that this can be distinguished from within the method. Indeed, it is a problem that a lot of people are facing, that onItemSelected is fired when the spinner is initiated. It seems that currently, the only workaround is to use an external variable for this.

private Boolean isUserAction = false;

...

public void onItemSelected( ... ) {

    if( isUserAction ) {
       // code for user initiated selection
    } else {
       // code for programmatic selection
       // also triggers on init (hence the default false)
    }

    // reset variable, so that it will always be true unless tampered with
    isUserAction = true;
}

public void myButtonClick( ... ) {
    isUserAction = false;
    mySpinner.setSelectedItem ( ... );
}
David Hedlund
Thanks David for the Reply.I am using the similar workaround.Wnated to know if adapter(Th) could help in this(Through its own listener).Or there would always be a"click event"on Spinner ,then followed by something changed/not.May be we could use that?
con_9