Android Spinner does not support rendering if generated programmatically in android 2.0

This code is written in the layout / xml file for spinner:

<Spinner android:id="@+id/planets_spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:entries="@array/planets_array" /> 

Java code inside onCreate (Bundle savedInstanceState) {} in MainActivity.java:

 ArrayList<String> spinnerArray = new ArrayList<String>(); spinnerArray.add("one"); spinnerArray.add("Jupiter"); spinnerArray.add("Mercury"); spinnerArray.add("Venus"); spinnerArray.add("Earth"); Spinner spinner = new Spinner(this); ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray); spinner.setAdapter(spinnerArrayAdapter); 

enter image description here What could be wrong?

+4
source share
1 answer

You are using the wrong resource ... the verification method you are using is for the drop-down resource only ...

should work (note simple_spinner_dropdown_item ! = simple_spinner_item ):

 ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray); spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
+3
source

Source: https://habr.com/ru/post/1502820/


All Articles