Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spinner Adapter Setting to spinner

Tags:

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);
}
like image 280
khalil Avatar asked Jan 09 '14 13:01

khalil


People also ask

How can I use more than one spinner in Android?

Clearly each of your adapters need to adapt a different set of String s, so you need to create an ArrayAdapter for each Spinner . A single OnItemSelectedListener will work for the 3 Spinners (as long as you set them). You can call getId() on the AdapterView<?>

How do you populate a spinner?

In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. To populate the spinner with a list of choices, you then need to specify a SpinnerAdapter in your Activity or Fragment source code.

What is a spinner adapter?

A spinner adapter allows to define two different views: one that shows the data in the spinner itself and one that shows the data in the drop down list when the spinner is pressed.

What is an android spinner?

Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.


1 Answers

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

like image 192
Rat-a-tat-a-tat Ratatouille Avatar answered Sep 27 '22 21:09

Rat-a-tat-a-tat Ratatouille