Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner : onItemSelected not called when selected item remains the same

I have a OnItemSelectedListener for my Spinner, but it is not called when the selected item is the same as the previous one. Apparently the OnClickListener is not an option for a Spinner. I need to catch everytime a user click on an item. Any idea?

Maybe the fact that this Spinner is inside the ActionBar disturbs normal behavior?

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {     inflater.inflate(R.menu.tracklist_menu, menu);     Spinner spinner = (Spinner) menu.findItem(R.id.option_ordering_spinner)             .getActionView();     spinner.setAdapter(mSpinnerAdapter);     spinner.setSelection(PrefsHelper.getOrderingSpinnerPos(prefs));     spinner.setOnItemSelectedListener(new OnItemSelectedListener() {          @Override         public void onItemSelected(AdapterView<?> parent, View view,                 int position, long id) {             String str = "selected";             System.out.println(str);             if (optionMenuInitialized) {                  switch (position) {                 case 0:                     // rdm                     getActivity()                             .sendBroadcast(                                     new Intent(                                             MyIntentAction.DO_RESHUFFLE_PLAYLIST));                     smp.setCurrentTracklistCursorPos(-1);                     trackAdapter.notifyDataSetChanged();                     break;                 case 1:                     // artist                     getActivity()                             .sendBroadcast(                                     new Intent(                                             MyIntentAction.DO_ORDER_PLAYLIST_BY_ARTIST));                     smp.setCurrentTracklistCursorPos(-1);                     trackAdapter.notifyDataSetChanged();                     break;                 case 2:                     // folder                     getActivity()                             .sendBroadcast(                                     new Intent(                                             MyIntentAction.DO_ORDER_PLAYLIST_BY_FOLDER));                     smp.setCurrentTracklistCursorPos(-1);                     trackAdapter.notifyDataSetChanged();                     break;                 }                 PrefsHelper.setOrderingSpinnerPos(prefEditor, position);                 prefEditor.commit();             }             optionMenuInitialized = true;         }          @Override         public void onNothingSelected(AdapterView<?> parent) {         }     }); } 
like image 565
elgui Avatar asked Jun 01 '12 16:06

elgui


People also ask

How can I get an event in android spinner when the current selected item is selected again?

getSelectedItem(), Toast.

Which function is used to get the selected item from the spinner?

getItemAtPosition(i).

How can I tell if spinner is clicked or not in android?

Let the Spinner's first value be something like "-please select-". when the user clicks on the next button perform validation and check whether the value of the selectedItem in the spinner is "-please select-" and if yes, then display a toast and ask the the user to select something from the spinner.


2 Answers

Ok, I finally found a solution, by creating my own class extending Spinner :

public class MySpinner extends Spinner { OnItemSelectedListener listener;  public MySpinner(Context context, AttributeSet attrs) {     super(context, attrs); }  @Override public void setSelection(int position) {     super.setSelection(position);     if (listener != null)         listener.onItemSelected(null, null, position, 0); }  public void setOnItemSelectedEvenIfUnchangedListener(         OnItemSelectedListener listener) {     this.listener = listener; } } 
like image 182
elgui Avatar answered Oct 04 '22 13:10

elgui


I found out this work instead of the one provided

/** Spinner extension that calls onItemSelected even when the selection is the same as its previous value */ public class NDSpinner extends Spinner {    public NDSpinner(Context context)   { super(context); }    public NDSpinner(Context context, AttributeSet attrs)   { super(context, attrs); }    public NDSpinner(Context context, AttributeSet attrs, int defStyle)   { super(context, attrs, defStyle); }    @Override public void   setSelection(int position, boolean animate)   {     boolean sameSelected = position == getSelectedItemPosition();     super.setSelection(position, animate);     if (sameSelected) {       // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now       getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());     }   }    @Override public void   setSelection(int position)   {     boolean sameSelected = position == getSelectedItemPosition();     super.setSelection(position);     if (sameSelected) {       // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now       getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());     }   } } 
like image 35
Eric Leong Zhia Choong Avatar answered Oct 04 '22 13:10

Eric Leong Zhia Choong