如何在 Spinner Android 中添加项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24825249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add item in Spinner Android
提问by MilanNz
How to add item from str in spinner? I tryed with ArrayList, ArrayAdapter but didn't work...
如何在微调器中从str添加项目?我尝试过 ArrayList、ArrayAdapter 但没有用...
String[] str={"item 1","item 2","item 3"};
Spinner s=(Spinner)findViewById(R.id.spinner);
回答by Setu
You can use simple default ArrayAdapter to achieve your requirement dynamically. Below is the code snippet you can follow and modify your onCreate Method to add values in the spinner.
您可以使用简单的默认 ArrayAdapter 来动态实现您的要求。以下是您可以遵循并修改 onCreate 方法以在微调器中添加值的代码片段。
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter;
List<String> list;
list = new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
list.add("Item 5");
adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
if you want the spinner values to be added statically you can add the array in the xml file and assign the attribute entries
for the spinner. below is the code snippet for that.
如果您希望静态添加微调器值,您可以在 xml 文件中添加数组并entries
为微调器分配属性。下面是代码片段。
Your layout xml file
您的布局 xml 文件
<Spinner android:id="@+id/spinner"
android:layout_width="fill_parent"
android:drawSelectorOnTop="true"
android:prompt="@string/spin"
android:entries="@array/items" />
In your arrays.xml file
在您的 arrays.xml 文件中
<string-array name="items">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
<item>Item 5</item>
</string-array>
回答by user1841702
The Android reference has a good example and instead of using:
Android 参考有一个很好的例子,而不是使用:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
as given in the example (this is because the Spinner
is populated from a pre-existing string-array resource defined in an xml file ) you could use the ArrayAdapter
constructor that accepts a List as its argument, as shown at:
如示例中所示(这是因为Spinner
从 xml 文件中定义的预先存在的字符串数组资源填充)您可以使用ArrayAdapter
接受 List 作为其参数的构造函数,如下所示:
You could do this like so:
你可以这样做:
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.your_layout,mYourList)
where mYourList could be an ArrayList<String>
where you can store the data that you want to populate your Spinner
with.
其中 mYourList 可以是ArrayList<String>
您可以存储要填充的数据的位置Spinner
。
回答by OrhanC1
Android Spinner
s use the SpinnerAdapter
class.
http://developer.android.com/reference/android/widget/SpinnerAdapter.html
AndroidSpinner
使用SpinnerAdapter
该类。
http://developer.android.com/reference/android/widget/SpinnerAdapter.html