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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:50:18  来源:igfitidea点击:

Android Spinner Adapter Setting to spinner

androidspinner

提问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 thisin 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 thisrefers to ActivityName.classor the context

不确定此代码位于哪个块或哪个类,但请确保this引用ActivityName.classthe context

回答by Larry Schiefer

It is most likely because you are using an ArrayAdapterrather than a SpinnerAdapter. ArrayAdapteris an indirect implementer of the SpinnerAdapterinterface 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而不是SpinnerAdapterArrayAdapterSpinnerAdapter接口的间接实现者,而不是声明它实现了接口的实现者。检查未定义的异常。它很可能会告诉您setAdapter(ArrayAdapter)没有为Spinner.