I have a spinner, created in an XML file:
<Spinner
android:id="@+id/unitSpinner"
android:entries="@array/units" />
With its entries defined in array.xml
<string-array name="units">
<item>g</item>
<item>kg</item>
<item>ml</item>
<item>l</item>
<item>szt.</item>
<item>op.</item>
</string-array>
Now in my Java file I want to create an ArrayList<String>
which contents after feeding it with Spinner
's entries would be:
["g", "kg", "ml", "l", "szt.", "op."]
My Java code looks like this:
Spinner unit = (Spinner) findViewById(R.id.unitSpinner);
ArrayList<String> array = new ArrayList<>();
//pass information from unit to array
This question differs from many questions like Android : Fill Spinner From Java Code Programmatically as I don't want to fill Spinner with an Array, but the opposite way.
You can get the list from the XML array directly. no need to get ti from the spinner
String[] ss = getResources().getStringArray(R.array.units);
ArrayList<String> array = Arrays.asList(words);
if this aproach does not work with you you can try to get all the items fom the spinner via loop
for( int i = 0 ; i< unit.getAdapter().getCount() ; i++ ){
array.add ( ss.getAdapter().getItem( i ) )
}
Hope this will help.