How can i populate a Spinner from String array
, I know i can do that from array.xml
like this code
:
ArrayAdapter<CharSequence> gameKindArray = ArrayAdapter.createFromResource(view.getContext(),R.array.game_kind, android.R.layout.simple_spinner_item);
gameKindArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gameKind.setAdapter(gameKindArray);
but when i have
String[] test=new String[]{"test1","test2"};
how i can change String[]
to ArrayAdapter
?!
Use ArrayAdapter this way
your string array
String[] test=new String[]{"test1","test2"};
your ArrayAdapter
ArrayAdapter<String> gameKindArray= new ArrayAdapter<String>(MyActivityClass.this,android.R.layout.simple_spinner_item, test);
gameKindArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gameKind.setAdapter(gameKindArray);
Try the below
ArrayAdapter<String> gameKindArray = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, test);
gameKind.setAdapter(adapter);
For more info check the docs.
http://developer.android.com/reference/android/widget/ArrayAdapter.html
http://developer.android.com/reference/android/widget/Spinner.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With