Android Spinner Adapter 设置为 Spinner
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21021565/
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 Spinner Adapter Setting to spinner
提问by khalil
I'm using eneter framework to process communication in my android application; the problem is when I'm trying to populate a spinner, setting the adapter to the spinner cause an undefined exception
我正在使用eneter框架来处理我的android应用程序中的通信;问题是当我尝试填充微调器时,将适配器设置为微调器会导致未定义的异常
Here the code
这里的代码
public void populateSpinner(TypedResponseReceivedEventArgs<String> arg1){
List<String> list = new ArrayList<String>();
String listf = arg1.getResponseMessage();
//sendToDebug(listf);
StringTokenizer tokenizer = new StringTokenizer(listf,",");
while(tokenizer.hasMoreElements()){
list.add((String)tokenizer.nextElement());
}
//EditText text = (EditText)findViewById(R.id.number2EditText);
//text.setText(list.size());
//text.setText(listf);
Spinner forfait = (Spinner)findViewById(R.id.forfaitsSpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
forfait.setAdapter(adapter);
}
回答by Rat-a-tat-a-tat Ratatouille
you are passing this
in the following piece of code,
您正在传递this
以下代码,
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,list);
Not sure in which block this code lies or which class, but ensure that this
refers to ActivityName.class
or the context
不确定此代码位于哪个块或哪个类,但请确保this
引用ActivityName.class
或 the context
回答by Larry Schiefer
It is most likely because you are using an ArrayAdapter
rather than a SpinnerAdapter
. ArrayAdapter
is an indirect implementer of the SpinnerAdapter
interface rather than one which declares that it implements the interface. Check the undefined exception. It is likely telling you that setAdapter(ArrayAdapter)
is not defined for Spinner
.
这很可能是因为您使用的是ArrayAdapter
而不是SpinnerAdapter
。 ArrayAdapter
是SpinnerAdapter
接口的间接实现者,而不是声明它实现了接口的实现者。检查未定义的异常。它很可能会告诉您setAdapter(ArrayAdapter)
没有为Spinner
.