Spinner component usage _android of Android programming development

Source: Internet
Author: User

This example describes the spinner component usage of Android programming. Share to everyone for your reference, specific as follows:

The spinner component group uses an adapter adapter to display a Drop-down list, and the following is an example of how the component is used

The first is the layout file Main.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
 android:orientation=" vertical "android:layout_width=" fill_parent " 
 android:layout_height=" Fill_parent "> 
 <spinner android:id=" @+id/spinner1 android:layout_width= "Fill_parent" 
  android: layout_height= "Wrap_content"/> <spinner android:id= "@+id/spinner2" android:layout_width= "Fill_parent" 
  android:layout_height= "wrap_content" android:layout_margintop= "20DP"/>
</LinearLayout> 

Because Simpadapter is used, the layout of the item to be written is as follows Item.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
 android:orientation=" Horizontal "android:layout_width=" fill_parent " 
 android:layout_height=" "Wrap_content" > 
 <imageview android:id= "@+id/ivlogo" android:layout_width= "60DP" 
  android:layout_ height= "60DP" android:src= "@drawable/icon" 
  android:paddingleft= "10DP"/> "<textview android:id= 
 " @+id /tvapplicationname "android:textcolor=" #000 " 
  android:layout_width=" wrap_content "android:layout_height=" fill _parent " 
  android:textsize=" 16DP "android:gravity=" center_vertical " 
  android:paddingleft=" 10DP "/> 
</LinearLayout> 

Here's the code:

Import java.util.ArrayList; 
Import Java.util.HashMap; 
Import java.util.List; 
Import Java.util.Map; 
Import android.app.Activity; 
Import Android.app.AlertDialog; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.widget.AdapterView; 
Import Android.widget.ArrayAdapter; 
Import Android.widget.SimpleAdapter; 
Import Android.widget.Spinner; 
Import Android.widget.AdapterView.OnItemSelectedListener; public class Main extends activity {@Override the public void OnCreate (Bundle savedinstancestate) {super.oncreate ( 
  Savedinstancestate); 
  Setcontentview (R.layout.main); 
  Gets the object Spinner spinner1 = (Spinner) Findviewbyid (r.id.spinner1); 
  string[] Applicationnames = new string[] {"Multifunction Calendar", "Eoemarket client", "play-resistant gravity block", "White Society", "program Terminator"}; arrayadapter<string> aaadapter = new Arrayadapter<string> (this, Android. 
  R.layout.simple_spinner_item, Applicationnames); Use the following code to bring the list items to the RadioButton component//Aaadapter.setdropdownviewresource (Android. R.laYout.simple_spinner_dropdown_item); 
  Spinner1.setadapter (Aaadapter); 
  Spinner Spinner2 = (Spinner) Findviewbyid (R.ID.SPINNER2); 
  Final list<map<string, object>> items = new arraylist<map<string, object>> (); 
  map<string, object> item1 = new hashmap<string, object> (); 
  Item1.put ("Ivlogo", R.drawable.calendar); 
  Item1.put ("Tvapplicationname", "multi-functional calendar"); 
  map<string, object> item2 = new hashmap<string, object> (); 
  Item2.put ("Ivlogo", R.drawable.eoemarket); 
  Item2.put ("Tvapplicationname", "Eoemarket client"); 
  Items.Add (ITEM1); 
  Items.Add (ITEM2); Simpleadapter simpleadapter = new Simpleadapter (this, items, R.layout.item, new string[] {"Ivlogo", "Tvapplicat 
  Ionname "}, new int[] {R.id.ivlogo, r.id.tvapplicationname}); 
  Spinner2.setadapter (Simpleadapter); Spinner2 Plus Listener Event Spinner2.setonitemselectedlistener (new Onitemselectedlistener () {@Override public void on Itemselected (adapterview<?> Parent, view view, int position, long ID) {New Alertdialog.builder (View.getcontext ()). Settitle ( Items.get (position). Get ("Tvapplicationname"). ToString ()). SetIcon (Integer.parseint items.get (positi 
   ON). Get (' Ivlogo '). toString ()). Show (); 
 @Override public void onnothingselected (adapterview<?> parent) {}});

 } 
}

I hope this article will help you with your Android programming.

Related Article

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.