Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate a Spinner from String array

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

like image 549
Saad Ahmad Avatar asked Jun 26 '13 04:06

Saad Ahmad


Video Answer


2 Answers

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); 
like image 187
Dixit Patel Avatar answered Oct 15 '22 13:10

Dixit Patel


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

like image 35
Raghunandan Avatar answered Oct 15 '22 14:10

Raghunandan