Here's how to set up a data source for a spinner control in a program by using a collection, in the following steps:
1. In the Android Studio interface, select Project, and then expand "Application", "res", "Layout", open Activity_main.xml, add a spinner control, the code is as follows:
<spinner android:id= "@+id/eduspinner" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" ></Spinner>
2, find the "app", "Java" under the Mainactivity.java file, define a few variables, the code is as follows:
Private list<charsequence> edulist = null; Private arrayadapter<charsequence> eduadapter = null; Private Spinner eduspinner= null;
3. After the OnCreate method of activity, add the following method:
Locate the Spinner control Eduspinner = (Spinner) Super.findviewbyid (r.id.eduspinner); Eduspinner.setprompt ("Please select your education:"); Edulist = new arraylist<charsequence> (); Edulist.add ("college"); Edulist.add ("undergraduate"); Edulist.add ("master"); Edulist.add ("other"); Eduadapter = new Arrayadapter<charsequence> (this,android. r.layout.simple_spinner_item,edulist); Eduadapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item); Eduspinner.setadapter (Eduadapter);
4, run the program, the spinner has been bound to the corresponding data, as shown in:
The data that is bound later can be obtained from the server by WebService and then bound to the spinner control.
Data binding implementation of spinner controls in Android Studio