2

On a button click, I want to load a spinner with data from a table. So I use a SimpleCursorAdapter which I pass to spinner.SetAdapter. After that, I do some other code. After all of this, the OnSelectedItem event of my spinner is fired and I don't know why (like a post event). I noticed that if I don't call spinner.SetAdapter, the event is not fired. To make is easier to understand, here is my code (simplified):

onClick{
    // Here I load the SimpleCursorAdapter before the next line
    spinner.SetAdapter(lSimpleCursorAdapter);
    DoSomeCode();
}
// Then after the OnClick, the OnSelectedItem event of my spinner is fired, I don't know why

Do someone knows what the problem would be?

Math
  • 389
  • 2
  • 4
  • 14
  • That is intended behaviour. – Shark May 27 '13 at 15:17
  • So there is no way to prevent this? I could use a boolean variable but I am afraid that it could create other random problems if in a particular case, the event is not fired after a setAdapter. – Math May 27 '13 at 17:26
  • Why would you like to avoid this? – Jorge Gil May 27 '13 at 19:18
  • I am using a spinner to have a list of elements that I can select, and I use a textbox that has the actual element selected. If I want to add a record, the text is on the new element (let's say "2"). If I add the record, I must reload the spinner so that the "2" appears in it. But when the onselecteditem event executes, it loads the selected element (let's say "1"). So if I reload the spinner and then I change my textbox to 2, after that the onselecteditem executes and it loads the "1" element, and then the textbox returns to "1". – Math May 27 '13 at 20:27
  • Restructure your logic to better lie on top of the event-driven system you're working in. Understand your events, why and where they come from. Adapt and structure your code likewise. Basically, your problem now is that you are really selecting "1" but as a programmer you think you're selecting "2" instead and now you're making a workaround for intended behavoiur. Instead, why not set selection to the new item programatically so that the OnSelectedItem has to fire for the "2" and not "1" ? – Shark May 28 '13 at 14:34
  • I can not select 2, it is not in the spinner yet because it is the next element to be inserted if the user add it by clicking the button. Even if it would be in the spinner before, I would still not be able select it because the event is fired after my code. The problem is that I would like to do code after the onSelectedItem event, which seems to be impossible. – Math May 28 '13 at 17:45
  • [http://stackoverflow.com/questions/2562248/android-how-to-keep-onitemselected-from-firing-off-on-a-newly-instantiated-spin](http://stackoverflow.com/questions/2562248/android-how-to-keep-onitemselected-from-firing-off-on-a-newly-instantiated-spin) – paul May 24 '14 at 05:45

0 Answers0