12

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 ?!

1

3 Answers 3

30

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); 
Sign up to request clarification or add additional context in comments.

1 Comment

I'm working with an integer array will this still work for that as well? if yes how do I do that?
2

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

Comments

1

you dont need to convert in any form just use your string array like below:

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

you can get easily into spinner.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.