Data binding implementation of spinner controls in Android Studio

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.