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

4

3 回答 3

30

以这种方式使用 ArrayAdapter

你的字符串数组

   String[] test=new String[]{"test1","test2"};

你的 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); 
于 2013-06-26T04:09:00.573 回答
2

试试下面

     ArrayAdapter<String> gameKindArray =  new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, test);   
     gameKind.setAdapter(adapter);

有关更多信息,请查看文档。

http://developer.android.com/reference/android/widget/ArrayAdapter.html

http://developer.android.com/reference/android/widget/Spinner.html

于 2013-06-26T04:09:42.717 回答
1

您不需要以任何形式进行转换,只需使用您的字符串数组,如下所示:

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

您可以轻松进入微调器。

于 2013-06-26T04:12:47.913 回答