How to make On Item Selected not automatically cho

2020-02-28 05:56发布

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 the spinner so when one of the names in the spinner is selected, a new window appears. However the OnItemSelected is automatically selecting the first item on the list when the activity starts and so the user does not have a chance to actually make a selection until the new window appears.

Here is the code:

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    startActivity(new Intent("com.lukeorpin.theappliancekeeper.APPLIANCESELECTED"));
    }

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

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

Here is the code for the rest of the spinner:

ArrayAdapter<String> appliancenameadapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, ApplianceNames); //Sets up an array adapter containing the values of the ApplianceNames string array
    applianceName = (Spinner) findViewById(R.id.spinner_name); //Gives the spinner in the xml layout a variable name
    applianceName.setAdapter(appliancenameadapter); //Adds the contents of the array adapter into the spinner

    applianceName.setOnItemSelectedListener(this);

4条回答
劳资没心,怎么记你
2楼-- · 2020-02-28 06:31

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...
        }

    }
查看更多
够拽才男人
3楼-- · 2020-02-28 06:34

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) {
      }
    });
  }
});
查看更多
别忘想泡老子
4楼-- · 2020-02-28 06:35

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.

查看更多
地球回转人心会变
5楼-- · 2020-02-28 06:39

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);}

                    }
查看更多
登录 后发表回答