Android:从数组以编程方式创建微调器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2784081/
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
Android: Create spinner programmatically from array
提问by Select0r
I'm all new to Android and I'm trying to create a spinner programmatically and feeding it with data from an array, but Eclipse gives me a warning that I can't handle.
我是 Android 的新手,我正在尝试以编程方式创建一个微调器并用数组中的数据提供它,但是 Eclipse 给了我一个我无法处理的警告。
Here's what I got:
这是我得到的:
This ArrayList holds the elements that should be in the spinner (gets filled from a file later on):
ArrayList<String> spinnerArray = new ArrayList<String>();
这个 ArrayList 包含应该在微调器中的元素(稍后从文件中填充):
ArrayList<String> spinnerArray = new ArrayList<String>();
This is code I found on a site which should create the spinner:
这是我在一个网站上找到的应该创建微调器的代码:
Spinner spinner = new Spinner(this);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item,
spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
Now the second line (ArrayAdapter...) gives me a warning in Eclipse saying "ArrayAdapter is a raw type... References to generic type ArrayAdapter<T> should be parameterized"
, I have no idea how to fix this (or what that means in the first place :) ).
现在第二行 (ArrayAdapter...) 在 Eclipse 中给我一个警告说"ArrayAdapter is a raw type... References to generic type ArrayAdapter<T> should be parameterized"
,我不知道如何解决这个问题(或者这首先意味着什么:))。
It's just a warning and the App seems to run alright, but I'd still like to understand what's wrong and fix it. Any hint is appreciated.
这只是一个警告,应用程序似乎运行正常,但我仍然想了解问题所在并修复它。任何提示表示赞赏。
Greetings, Select0r
问候, Select0r
回答by Brandon O'Rourke
ArrayAdapter<String>
should work.
ArrayAdapter<String>
应该管用。
i.e.:
IE:
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item,
spinnerArray); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
回答by Yann Masoch
In the same way with Array
以同样的方式与数组
// Array of choices
String colors[] = {"Red","Blue","White","Yellow","Black", "Green","Purple","Orange","Grey"};
// Selection of the spinner
Spinner spinner = (Spinner) findViewById(R.id.myspinner);
// Application of the Array to the Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colors);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spinner.setAdapter(spinnerArrayAdapter);
回答by Bruno Bieri
This worked for me with a string-arraynamed shoes
loaded from the projects resources:
这对我使用了从项目资源加载的名为字符串数组的方法shoes
:
Spinner spinnerCountShoes = (Spinner)findViewById(R.id.spinner_countshoes);
ArrayAdapter<String> spinnerCountShoesArrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_dropdown_item,
getResources().getStringArray(R.array.shoes));
spinnerCountShoes.setAdapter(spinnerCountShoesArrayAdapter);
This is my resource file (res/values/arrays.xml
) with the string-arraynamed shoes
:
这是我的资源文件 ( res/values/arrays.xml
),其字符串数组名为shoes
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="shoes">
<item>0</item>
<item>5</item>
<item>10</item>
<item>100</item>
<item>1000</item>
<item>10000</item>
</string-array>
</resources>
With this method it's easier to make it multilingual (if necessary).
使用这种方法可以更容易地使其成为多语言(如有必要)。
回答by Opy
This actually worked for me
这实际上对我有用
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
spinner = (Spinner) findViewById( R.id.spinner );
spinner.setAdapter(spinnerArrayAdapter);
回答by Arish Khan
this work for me:-
这对我有用:-
String[] array = {"A", "B", "C"};
String abc = "";
Spinner spinner = new Spinner(getContext());
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, array); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
I am using a Fragment.
我正在使用片段。
回答by Micer
In Kotlin language you can do it in this way:
在 Kotlin 语言中,您可以这样做:
val values = arrayOf(
"cat",
"dog",
"chicken"
)
ArrayAdapter(
this,
android.R.layout.simple_spinner_item,
values
).also {
it.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = it
}