2

I have two spinners, spinner A and spinner B. When the user changes A, B gets updated with a full new set of data. I also implemented a callback for B to use setOnItemSelectedListener so that I can modify some objects in another class whenever B is changed by the user.

B.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        mComponentColor.setSelection(position);
        mCompColorAsBuilt[mComponent.getComponentSelection()] = position;
        setColor();     
    }
});

The problem I've ran into is that I really don't want these objects to change unless the user was the one who changed the spinner. Because I automatically populate B based on A's selection, B's callback is invoked when the users changes A.

Any thoughts how I could deal with this situation?

user432209
  • 20,007
  • 10
  • 56
  • 75
  • 1
    Read this, it might help: http://stackoverflow.com/questions/4206047/spinner-how-may-i-distinguish-users-actions-from-computers-actions-in-a-onite/4206541#4206541 – blindstuff Dec 02 '10 at 02:11
  • Thanks, but that will be a last resort for me. There has got to be a better way. – user432209 Dec 02 '10 at 02:17
  • Create wrapper and make application call one method and user another? Much like boolean flags but with a chance to document this design decision. Myself I dont like the whole idea :) – Alex Nikolaenkov Dec 02 '10 at 04:40
  • I've used flags in the past as well. I know what you mean that's it's not always the prettiest solution. Another approach I've used to avoid unwanted notifications being received is by registering and unregistering listeners at appropriate times. – dhaag23 Dec 02 '10 at 07:54
  • Interesting thoughts. Thanks folks. Going to ponder these for a while. – user432209 Dec 02 '10 at 12:14

1 Answers1

0

I basically ended up solving this using a similar method to the flags method mentioned above, but did it inside the object that is adjusted when the onClickListener methods are invoked.

Basically by remembering what the last state of the spinner is, I do a check before I adjust any objects within it. If the state is the same, nothing gets changed. Fortunately all the object adjustments were done by another class, which made it relatively easy to handle. I'm not overly happy with it, but I think it was ultimately better than using flags within the activity class.

Thanks to those that contributed.

user432209
  • 20,007
  • 10
  • 56
  • 75