How to make On Item Selected not automatically choose the first entry

后端 未结 4 631
小蘑菇
小蘑菇 2020-12-30 07:09

I have created a spinner which is automatically updated with appliance names when a person adds an appliance using an array adapter. I created an OnItemSelected method with

4条回答
  •  爱一瞬间的悲伤
    2020-12-30 07:55

    If you are trying to avoid the initial call to your listener's onItemSelected() method, another option is to use post() to take advantage of the view's message queue. The first time the spinner checks for your listener it won't be set yet.

    // Set initial selection
    spinner.setSelection(position);
    
    // Post to avoid initial invocation
    spinner.post(new Runnable() {
      @Override public void run() {
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView parent, View view, int position, long id) {
            // Only called when the user changes the selection
          }
    
          @Override
          public void onNothingSelected(AdapterView parent) {
          }
        });
      }
    });
    

提交回复
热议问题