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
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);
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);
This worked for me with a string-array named \"shoes
\" loaded from the projects 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:
<?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 easier to make it multilingual (if necessary).
This actually worked for me
Spinner spinner = new Spinner(this);
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) findViewById( R.id.spinner );
spinner.setAdapter(spinnerArrayAdapter);
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.