Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selecting the first item programmatically #12

Closed
jeffguo17 opened this issue May 9, 2016 · 5 comments
Closed

Selecting the first item programmatically #12

jeffguo17 opened this issue May 9, 2016 · 5 comments

Comments

@jeffguo17
Copy link

jeffguo17 commented May 9, 2016

How do you select the first item in the spinner?

I tried doing the following,

spinner.setOnItemListeners(..){ ... };

spinner.setSelectedIndex(2);
spinner.setSelected(true);

but it doesn't seem to be working.

Thanks!

@jaredrummler
Copy link
Owner

spinner.setSelectedIndex(int) should select that item. Make sure you invoke this method after setting the items in the spinner. For example, if I set the selected index to 1 in the sample, then "Jelly Bean" would be selected instead of "Ice Cream Sandwich".

MaterialSpinner spinner = (MaterialSpinner) findViewById(R.id.spinner);
spinner.setItems("Ice Cream Sandwich", "Jelly Bean", "KitKat", "Lollipop", "Marshmallow");
spinner.setSelectedIndex(1); // <--- Set the item programmatically
spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {

  @Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
    Snackbar.make(view, "Clicked " + item, Snackbar.LENGTH_LONG).show();
  }
});
spinner.setOnNothingSelectedListener(new MaterialSpinner.OnNothingSelectedListener() {

  @Override public void onNothingSelected(MaterialSpinner spinner) {
    Snackbar.make(spinner, "Nothing selected", Snackbar.LENGTH_LONG).show();
  }
});

@jeffguo17
Copy link
Author

jeffguo17 commented May 12, 2016

The method setSelectedIndex just chooses the item to appear as the first item, but I would like to actually select the item as if it would call on onItemSelected.

Thanks again, very nice spinner!

@jaredrummler
Copy link
Owner

You should just run whatever code is in your listener when you select the item. I don't know why this would be needed. If you really feel like this is necessary please provide an example.

@tomridder
Copy link

what he means is that when he use spinner.setSelectedIndex(1);,it justchoose the item to appear as the first item,but it didn't execute the selected event.
I have the same problem,please fix that to make this project better.Thanks man.very nice Spinner!

@ismail0234
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants