An example analysis of spinner control usage in the development of Android programming _android

Source: Internet
Author: User
Tags event listener

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

Drop-down list Spinner,spinner is a control that can only select one item at a time for all items. Its entry comes from the adapter associated with it.

The use of spinner can greatly improve the experience of users. When you need a user selection, you can provide a drop-down list of all the optional items listed. For the user to choose.

One, using arrays as data sources

Layout file:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" 
 xmlns:tools= "http:// Schemas.android.com/tools " 
 android:layout_width=" match_parent " 
 android:layout_height=" Match_parent " 
 android:paddingbottom= "@dimen/activity_vertical_margin" 
 android:paddingleft= "@dimen/activity_ Horizontal_margin " 
 android:paddingright=" @dimen/activity_horizontal_margin " 
 android:paddingtop=" @dimen /activity_vertical_margin " 
 tools:context=". Mainactivity "> 
 <spinner 
  android:id=" @+id/spinner1 "android:layout_width=" Match_parent " 
  android:layout_height= "wrap_content" 
  android:layout_alignparentleft= "true" 
  android:layout_ Alignparenttop= "true"/> 
</RelativeLayout>

Mainactivity.java

Package Com.example.lesson16_spinner; 
Import java.util.ArrayList; 
Import Java.util.HashMap; 
Import java.util.List; 
Import Java.util.Map; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.view.Menu; 
Import Android.view.View; 
Import Android.widget.AdapterView; 
Import Android.widget.ArrayAdapter; 
Import Android.widget.SimpleAdapter; 
Import Android.widget.Spinner; 
Import Android.widget.Toast; 
 public class Mainactivity extends activity {//Declare Spinner object private Spinner Spinner; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.activity_main); 
  Spinner = (spinner) Findviewbyid (R.id.spinner1); 
  Use an array as the data source final String arr[] = new string[] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Week Seven"}; Adpater object Arrayadapter<string> arrayadapter = new Arrayadapter<string> (this, Android. 
  R.layout.simple_spinner_item, arr); List<map<string, Object>> daTa = new arraylist<map<string, object>> (); 
  map<string, object> map = new hashmap<string, object> (); 
  Map.put ("logo", r.drawable.ic_launcher); 
  Map.put ("text", "xxxx description operation"); 
  map<string, object> map2 = new hashmap<string, object> (); 
  Map2.put ("logo", r.drawable.ic_launcher); 
  Map2.put ("text", "xxx description xxx"); 
  Data.add (map); 
  Data.add (MAP2); Simpleadapter simpleadapter = new Simpleadapter (this, data, R.layout.activity_item, new string[] {"logo", "Text"}, new I 
  Nt[] {r.id.imageview1, r.id.textview1}); 
  Sets the displayed data Spinner.setadapter (arrayadapter); 
  Spinner.setadapter (Simpleadapter); Toast.maketext (Getapplicationcontext (), "main Thread" + spinner.getitematposition (spinner.getselecteditemposition ( 
  ), Toast.length_long). Show (); Register Event Spinner.setonitemselectedlistener (new Adapterview.onitemselectedlistener () {@Override public void Onit 
  Emselected (adapterview<?> Parent, view view, int position, long ID) {  String content = Arr[position]; 
    Spinner Spinner = (Spinner) parent; 
   Toast.maketext (Getapplicationcontext (), "xxxx" + spinner.getitematposition (position), Toast.length_long). Show (); @Override public void onnothingselected (adapterview<?> parent) {Toast.maketext (getapplicationcontext 
   (), "No change of Processing", Toast.length_long). Show (); 
 } 
  }); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar 
  If it is present. 
  Getmenuinflater (). Inflate (R.menu.main, menu); 
 return true;

 } 
}

The effect is as follows:

Ii. using XML as a data source (not my Code)

Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> 
<resources> 
 <string-array name= "Plantes" > 
  <item>NOKIA</item> 
  <item>MOTO</item> 
  <item>HTC</item> 
  <item >LG</item> 
  <item> Other </item> 
 </string-array> 
</resources>

Java code:

Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.widget.AdapterView; 
Import Android.widget.AdapterView.OnItemSelectedListener; 
Import Android.widget.ArrayAdapter; 
Import Android.widget.Spinner; 
Import Android.widget.TextView; 
 public class Spinneractivity extends activity {private TextView view2; 
 Private Spinner Spinner2; 
 Private Arrayadapter Adapter2; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.oncreate (s 
  Avedinstancestate); 
  Setcontentview (R.layout.spinner); 
  Spinner2 = (Spinner) Findviewbyid (R.ID.SPINNER02); 
  View2 = (TextView) Findviewbyid (r.id.spinnertext02); Connect the optional content to the Arrayadapter Adapter2 = Arrayadapter.createfromresource (this, R.array.plantes, Android. 
  R.layout.simple_spinner_item); Set the style of the Drop-down list Adapter2.setdropdownviewresource (Android. 
  R.layout.simple_spinner_dropdown_item); Add Adapter2 to spinner Spinner2.setadapter (adapter2);
  Add Event Spinner Event Listener Spinner2.setonitemselectedlistener (New Spinnerxmlselectedlistener ()); 
 Set default value spinner2.setvisibility (view.visible); Use XML to manipulate class Spinnerxmlselectedlistener implements onitemselectedlistener{public void onitemselected (adapte 
  Rview<?> arg0, View arg1, int arg2, long arg3) {View2.settext ("What kind of cell phone do you use:" +adapter2.getitem (arg2));

 public void onnothingselected (Adapterview<?> arg0) {}}}

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.