2

In Android Spinner, I can set selection to a particular Item in the code (using setSelection(int)). This will end up calling OnItemSelectedListener, which is fine.

But when the user selects an item from the screen, by clicking the spinner and the item, I want to handle this as a different event, because the logic in my application should do different things.

How to achieve this? To summarize, My Spinner should do this

If Value set from the code {
//do this..
}
else if user select a value {
//do that..
}
franklins
  • 3,710
  • 6
  • 41
  • 56
  • So I am expecting something like OnItemChange. I tried using OnItemClick, LongClick etc.. which just threw runtimeException. – franklins Nov 01 '10 at 15:33
  • You're expecting different functionality than the spinner provides. You're free to write your own spinner class though. – Falmarri Nov 01 '10 at 16:07
  • This might be what you are looking for: http://stackoverflow.com/a/15675409 – showp1984 Mar 28 '13 at 15:04

2 Answers2

1

Extend Spinner and override setSelection(int). Insert functionality you want to happen in this case and then call super.setSelection(int) to keep the Spinner working normally.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • Thanks for your reply. But still this will end up calling onItemSelected method, which has a logic to perform when user selects from the screen. So I need a way to know in onItemSelected method that this was set in the code or set by the user. – franklins Nov 01 '10 at 16:02
  • So you have different code that executes in each case? Just move one of the code to .setSelection(). – Peter Knego Nov 01 '10 at 16:14
  • Hack: or change the `int` in `setSelection(int)` to `int + 10000` and you will know that this came from code. Not sure if it would work. – Peter Knego Nov 01 '10 at 16:16
1

I ended up using a global variable to indicate me who is calling the onItemSelected method. Looks like solved my purpose. But any better solutions are welcome.

franklins
  • 3,710
  • 6
  • 41
  • 56