Android Spinner Adapter Setting to spinner

2019-06-15 02:10发布

问题:

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

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

回答1:

you are passing this in the following piece of code,

 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



回答2:

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.