cspam
cspam

Reputation: 2941

Adding Spinner View in Android without using an XML layout

I am trying to create a spinner in my code without using an xml layout at all. It seems that you need an ArrayAdapter with the spinner but the constructors all want a resource id in its set of constructors. Well, when not using XML layout i dont see how I can get a resource id.

        Spinner spinner = new Spinner(this);

        List<String> spinnerList = new ArrayList<String>();
        spinnerList.add("Ice Cream Sandwich");
        spinnerList.add("Hushpuppies");
        spinnerList.add("Pickled Pigs Feet");
        spinnerList.add("Cupcakes");
        spinnerList.add("Chocolate Covered Pretzels");

        ArrayAdapter<String> mySpinnerAdapter = new ArrayAdapter<String>(this, ??);

        //spinner.setAdapter(mySpinnerAdapter);

how should i instantiate an ArrayAdapter here? thanks all in advance and hopefully i didn't make anyone too hungry. :)

EDIT: i think i have it now - will test and see...

 ArrayAdapter<String> mySpinnerAdapter = new ArrayAdapter<String>(this,  android.R.layout.simple_spinner_item, spinnerList);
 mySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

 spinner.setAdapter(mySpinnerAdapter);

Upvotes: 2

Views: 1063

Answers (2)

Christian Jb
Christian Jb

Reputation: 125

Try this

Spinner comboAutos = (Spinner) findViewById(R.id.spinnerCarros); comboAutos.setAdapter(new ArrayAdapter<String>( MyActivity.this, android.R.layout.simple_spinner_dropdown_item, list));

where list is a List<String>list with the values that you want :)

Hope it helps you!

Upvotes: 0

cspam
cspam

Reputation: 2941

i was able to use the android.R.layout and a subclass to solve what i needed to here

Upvotes: 1

Related Questions