How to populate a Spinner from String array

Saad Ahmad picture Saad Ahmad · Jun 26, 2013 · Viewed 21.5k times · Source

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

Answer

Dixit Patel picture Dixit Patel · Jun 26, 2013

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