211

I'm all new to Android and I'm trying to create a spinner programmatically and feeding it with data from an array, but Eclipse gives me a warning that I can't handle.

Here's what I got:

This ArrayList holds the elements that should be in the spinner (gets filled from a file later on):

ArrayList<String> spinnerArray = new ArrayList<String>();

This is code I found on a site which should create the spinner:

Spinner spinner = new Spinner(this);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
                android.R.layout.simple_spinner_dropdown_item,
                spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);

Now the second line (ArrayAdapter...) gives me a warning in Eclipse saying "ArrayAdapter is a raw type... References to generic type ArrayAdapter<T> should be parameterized", I have no idea how to fix this (or what that means in the first place :) ).

It's just a warning and the App seems to run alright, but I'd still like to understand what's wrong and fix it. Any hint is appreciated.

Greetings, Select0r

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Select0r
  • 12,234
  • 11
  • 45
  • 68

6 Answers6

402

ArrayAdapter<String> should work.

i.e.:

Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
            (this, android.R.layout.simple_spinner_item,
           spinnerArray); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
                                                     .simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter); 
abbood
  • 23,101
  • 16
  • 132
  • 246
Brandon O'Rourke
  • 24,165
  • 16
  • 57
  • 58
  • 1
    I tried that but the error just changes slightly :) `Type safety: The expression of type ArrayAdapter needs unchecked conversion to conform to ArrayAdapter` – Select0r May 06 '10 at 20:42
  • 3
    Commented to quickly while you edited your post :) I missed the second ``, your code works now, thanks a lot! – Select0r May 06 '10 at 20:43
  • 24
    use `ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(activity, R.layout.simple_spinner_item); spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);` or else a radio button may show up in the spinner on some devices. – Ken Jan 25 '12 at 02:46
  • your answere was helpful and can you please tell me that if i am using two spinners and i want if 1st spinner only then second should work –  Jul 19 '12 at 07:39
  • 1
    it also describes this method in the android documentation: http://developer.android.com/guide/topics/ui/controls/spinner.html – WOUNDEDStevenJones Oct 04 '12 at 21:00
114

In the same way with Array

// Array of choices
String colors[] = {"Red","Blue","White","Yellow","Black", "Green","Purple","Orange","Grey"};

// Selection of the spinner
Spinner spinner = (Spinner) findViewById(R.id.myspinner);

// Application of the Array to the Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,   android.R.layout.simple_spinner_item, colors);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spinner.setAdapter(spinnerArrayAdapter);
Yann Masoch
  • 1,628
  • 1
  • 15
  • 20
68

The following 3 lines of code worked for me with a string-array named shoes loaded from the project resources:

Spinner              spinnerCountShoes = (Spinner)findViewById(R.id.spinner_countshoes);
ArrayAdapter<String> spinnerCountShoesArrayAdapter = new ArrayAdapter<String>(
                     this,
                     android.R.layout.simple_spinner_dropdown_item, 
                     getResources().getStringArray(R.array.shoes));
spinnerCountShoes.setAdapter(spinnerCountShoesArrayAdapter);

This is my resource file (res/values/arrays.xml) with the string-array named shoes:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="shoes">
        <item>0</item>
        <item>5</item>
        <item>10</item>
        <item>100</item>
        <item>1000</item>
        <item>10000</item>
    </string-array>
</resources>

With this method it's very easy to make it multilingual, if necessary.

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
  • error: ArrayAdapter requires the resource ID to be a TextView – Sadique Khan Sep 15 '22 at 06:29
  • Seems you get an error trying out an answer I wrote almost 10 years ago. Maybe it's a good time to ask this question here on stackoverflow with your specific example. – Bruno Bieri Sep 15 '22 at 09:54
36

This actually worked for me

    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item, spinnerArray);
    spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
     
    Spinner spinner = (Spinner) findViewById( R.id.spinner );
    spinner.setAdapter(spinnerArrayAdapter);
Sid110307
  • 497
  • 2
  • 8
  • 22
Opy
  • 2,119
  • 3
  • 18
  • 22
  • 7
    What's the purpose of this `Spinner spinner = new Spinner(this);` when you do this `spinner = (Spinner) findViewById( R.id.spinner );` – mr5 Oct 26 '14 at 11:51
  • why error: no suitable constructor found for ArrayAdapter(,int,DetailData) constructor ArrayAdapter.ArrayAdapter(Context,int,int,List) is not applicable ? – danangindra Mar 18 '16 at 03:25
7

this work for me:-

String[] array = {"A", "B", "C"};
String abc = "";


Spinner spinner = new Spinner(getContext());
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, array); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);

I am using a Fragment.

Arish Khan
  • 81
  • 1
  • 4
5

In Kotlin language you can do it in this way:

val values = arrayOf(
    "cat",
    "dog",
    "chicken"
)

ArrayAdapter(
    this,
    android.R.layout.simple_spinner_item,
    values
).also {
    it.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    spinner.adapter = it
}
Micer
  • 8,731
  • 3
  • 79
  • 73