Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is onNothingSelected method needed in spinner listener?

native English speaker, so I'd say sorry about my bad English skills to you guys.

I've been studing Android since 5 weeks ago. I tried to implement a spinner and my mentor asked why the onNothingSelected method is needed. I had nothing to say.

So, why do I need that method?? Can you reply it?

Following code is my spinner. It does correctly what I intended.

 public class SpinnerViewPractice extends Activity {     private Spinner spinner;     private String spinner_value = "";     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         requestWindowFeature(Window.FEATURE_NO_TITLE);         setContentView(R.layout.spinnerviewpractice);          spinner = (Spinner)findViewById(R.id.spinner1);          String[] str = {"","good", "dislike", "like", "hate", "moderate"};         spinner.setPrompt("Set Text");         ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, str);          spinner.setAdapter(list);         spinner.setOnItemSelectedListener(new OnItemSelectedListener() {              @Override             public void onItemSelected(AdapterView<?> arg0, View arg1,                     int arg2, long arg3) {                 TextView tv = (TextView)arg1;                 spinner_value = tv.getText().toString();                 if(spinner_value.length() == 0)                 {                     spinner_value = "Nothing";                                   }                 Toast.makeText(SpinnerViewPractice.this, spinner_value, Toast.LENGTH_SHORT).show();             }              @Override             public void onNothingSelected(AdapterView<?> arg0) {                 Toast.makeText(SpinnerViewPractice.this, "NothingSelected", Toast.LENGTH_SHORT).show();             }                    });     } } 
like image 242
Won Chul Jo Avatar asked May 08 '13 11:05

Won Chul Jo


1 Answers

As the documentation describes:

Callback method to be invoked when the selection disappears from this view. The selection can disappear for instance when touch is activated or when the adapter becomes empty.

This means that the method is called whenever the currently selected item is removed from the list of available items. As the doc describes, this can occur under different circumstances, but generally if the adapter is modified such that the currently selected item is no longer available then the method will be called.

This method may be used so that you can set which item will be selected given that the previous item is no longer available. This is instead of letting the spinner automatically select the next item in the list.

like image 152
TheIT Avatar answered Sep 19 '22 13:09

TheIT