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

后端 未结 4 608
小蘑菇
小蘑菇 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:49

    It worked for me,

    private boolean isSpinnerInitial = true;

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
    
            if(isSpinnerInitial)
            {
                isSpinnerInitial = false;
            }
            else  {
                // do your work...
            }
    
        }
    
    0 讨论(0)
  • 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) {
          }
        });
      }
    });
    
    0 讨论(0)
  • 2020-12-30 07:59

    Does anyone know a way in which the first item on the list wont be automatically selected?

    There is always a selection on Spinner, and you cannot change that.

    IMHO, you should not be using a Spinner to trigger starting an activity.

    That being said, you can use a boolean to track whether this is the first selection event, and ignore it if it is.

    0 讨论(0)
  • 2020-12-30 08:10

    Declare variable isSpinnerInitial then make Make a Selection as your default selection

    spinnertaggeview.setSelection(-1); does not make selection as -1 or everything unselected as we do in .Net or other language . So you can ignore thatline.

    testStringArrayListinside.add("Make a Selection");
    ADD this line so that this is selected by default and user never selects it 
    
    testStringArrayList = (ArrayList<String>) ClinqLinX.get("Tag");
                    boolean added = false;
                 testStringArrayListinside.add("Make a Selection");
                    for (String s : testStringArrayList) {
                        if (s != null || s != "") {
                            String[] results = s.split(","); // split on commas
    
                            for (String string : results) {
    
                                testStringArrayListinside.add(string);
                                Toast.makeText(getContext(), string, Toast.LENGTH_SHORT).show();
                                added = true;
                            }
                        }
    
                    }
                    if (added == false) {
                        testStringArrayListinside.add("Not tagged details found");
                    }
    
                    spinnertaggeview.refreshDrawableState();
    
                }
    
                // Adapter: You need three parameters 'the context, id of the layout (it will be where the data is shown),
                // and the array that contains the data
                if (testStringArrayListinside.size() > 0) {
                    adapter = new ArrayAdapter<String>(this.getContext(),android.R.layout.select_dialog_singlechoice, testStringArrayListinside);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    // Here, you set the data in your ListView
                    spinnertaggeview.setAdapter(adapter);
                     isSpinnerInitial = true;
    
                    spinnertaggeview.setSelection(-1);
                    spinnertaggeview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
                        @Override
                        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                                   int arg2, long arg3) {
                            if (isSpinnerInitial){
    
                                isSpinnerInitial = false;
    
                                return;}
                            else{
                            TextView tv = (TextView) arg1;
                            String spinner_value = tv.getText().toString();
                            if (spinner_value.length() == 0) {
                                spinner_value = "Nothing";
                            }
                            String strusername = spinner_value;//As you are using Default String Adapter
                            Toast.makeText(getContext(), strusername, Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(spinnertaggeview.getContext(), Userdetails.class);
                            intent.putExtra("Username", strusername);
                            spinnertaggeview.getContext().startActivity(intent);}
    
                        }
    
    0 讨论(0)
提交回复
热议问题