ganfra / MaterialSpinner

Spinner with Material Design - Down to API 14
Apache License 2.0
946 stars 214 forks source link

SetSelection not working. #129

Open sleroy18 opened 6 years ago

sleroy18 commented 6 years ago

`ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, testArr);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); isBuying.setAdapter(adapter); isBuying.setSelection(1); ` will not work but the regular spinner

`isRenting = findViewById(R.id.rentingSpinner);

isRenting.setAdapter(adapterYesNo); isRenting.setSelection(1);`

does work The material spinner is set up correctly in the layout file. There is a hint associated with both spinners. The material spinner stays at the "hint" selection where the regular spinner is set to "No" which is position 1 in the array. I know I am using an array-string resource only for the regular spinner and I am converting that resource to an actual array for the material spinner.

Nothing I try seems to work.

Is there a work-around for setting the position in these "material spinners" it is not just this use of it, it is all!

sleroy18 commented 6 years ago

** UPDATE*** As a last ditch effort I went to set the text instead realizing that this was not a traditional spinner. The workaround I found to pre-set the selection is to use the adapter to get the text at position X and then set that text to the "spinners".

I still wish there was some functionality that did this already or better yet something much more clean.

museady commented 5 years ago

How did you set the selection on the spinner exactly ?

kevinMartellotti commented 5 years ago

For me the solution was to put the variable you want it to initialize to as the text programatically: spinner.setText(yourVariable; . Then add an onclick Listener to the spinner and match your variable to the spinner spinner.setOnItemClickListener((parent, view1, position, id) -> yourVariable=yourArray[position]); Then if you wanna upload it to a a database, for example, use that variable as the uploaded, one, as getting the spinnerText will give you the first, even tho you put see it like the third maybe.