1

I have problem in listening user interaction of the spinner when item selected. I know the below listener work perfect

spinner.setOnItemSelectedListener(this);

@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
    // your code here
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
    // your code here
}

but In my application scenario I will change the spinner value through program,that time also it fire the onitemselected event.but I need this event should fired only when user interaction happened on the spinner

Note:I am not talking on initial spinner value selected item.

Please Let me know how can I accomplish this

Thanks in advance, Naveenkumar.R

Fortega
  • 19,463
  • 14
  • 75
  • 113
naveen
  • 1,451
  • 6
  • 21
  • 27

2 Answers2

2
tempListener = spinner.getOnItemSelectedListener();
spinner.setOnItemSelectedListener(null);
//change the spinner value...
spinner.setOnItemSelectedListener(tempListener)

More elegantly, you could subclass the Spinner class, and create 'disableOnItemSelectedListener()' and 'enableOnItemSelectedListener()' methods, which do the same as the code above.

Fortega
  • 19,463
  • 14
  • 75
  • 113
  • Its working fine but I need to give some delay before setting the listener back.Let me know if any other solution available without setting the delay – naveen Jun 06 '12 at 15:23
  • It has some other problem I have resolved.Now its working without setting the delay.Thank you – naveen Jun 07 '12 at 05:43
  • The problem is that the spinner animates and does the callback when it's done. spinner.setSelection(i, false); will disable animation and likewise a delayed callback. – Renate Jun 15 '12 at 00:14
0

Undesired calls to onItemSelected can be avoided using a simple technique I describe in the following link. This way, you can yourself setSelection anytime without a worry. Check the accepted answer to this question:

Undesired onItemSelected calls

Community
  • 1
  • 1
Vedavyas Bhat
  • 2,068
  • 1
  • 21
  • 31