Select a spinner from the list

Source: Internet
Author: User

1. Only use xml configuration to display the list

Add arrays. XML to RES \ values

1 <? XML version = "1.0" encoding = "UTF-8"?> 2 <resources> 3 4 <string-array name = "books"> 5 <item> odd words </item> 6 <item> highlight </item> 7 <item> rain currently, </item> 8 <item> is becoming more and more consistent in </item> 9 </string-array> 10 11 </resources>

Add a spinner to the XML file on the home page.

1     <Spinner2         android:id="@+id/spi3"3         android:layout_width="match_parent"4         android:layout_height="wrap_content"5         android:entries="@array/books" >6     </Spinner>

Effect

 

2. Use the background to dynamically add

Layout:

1     <Spinner2         android:id="@+id/spi"3         android:layout_width="match_parent"4         android:layout_height="wrap_content" >5     </Spinner>

Add data class

1 package COM. example. myadapter; 2 3 Import Java. util. arraylist; 4 Import Java. util. hashmap; 5 import Java. util. list; 6 Import Java. util. map; 7 8 Import COM. example. androidspinner. r; 9 10 public class myadapter {11 12 public myadapter () {13 // todo auto-generated constructor stub14} 15 16 public static list <string> getdata () {17 list <string> List = new arraylist <string> (); 18 list. add ("Beijing"); 19 list. add ("Wuhan"); 20 list. add ("Guangzhou"); 21 return list; 22} 23 24}

Add data binding in the main program

 1 package com.example.androidspinner; 2  3 import java.util.List; 4 import java.util.Map; 5  6 import com.example.myadapter.Myadapter; 7  8 import android.os.Bundle; 9 import android.app.Activity;10 import android.view.Menu;11 import android.view.View;12 import android.widget.AdapterView;13 import android.widget.AdapterView.OnItemSelectedListener;14 import android.widget.ArrayAdapter;15 import android.widget.SimpleAdapter;16 import android.widget.Spinner;17 18 public class AndroidSpinnerActivity extends Activity {19     private Spinner spi;20 21     @Override22     protected void onCreate(Bundle savedInstanceState) {23         super.onCreate(savedInstanceState);24         setContentView(R.layout.activity_android_spinner);25         spi = (Spinner) this.findViewById(R.id.spi);26         List<String> list = Myadapter.getData();27         ArrayAdapter<String> adapter = new ArrayAdapter<String>(28                 AndroidSpinnerActivity.this,29                 android.R.layout.simple_spinner_item, list);30         spi.setAdapter(adapter);31 32                 }33 34           
40 }41 42 @Override43 public boolean onCreateOptionsMenu(Menu menu) {44 // Inflate the menu; this adds items to the action bar if it is present.45 getMenuInflater().inflate(R.menu.activity_android_spinner, menu);46 return true;47 }48 49 }

3. Add the option icon

Layout:

1     <Spinner2         android:id="@+id/spi2"3         android:layout_width="match_parent"4         android:layout_height="wrap_content" >5     </Spinner>

Add data class

1 package COM. example. myadapter; 2 3 Import Java. util. arraylist; 4 Import Java. util. hashmap; 5 import Java. util. list; 6 Import Java. util. map; 7 8 Import COM. example. androidspinner. r; 9 10 public class myadapter {11 12 public myadapter () {13 // todo auto-generated constructor stub14} 15 16 17 public static list <Map <string, object> getmapdata () {18 list <Map <string, Object> List = new arraylist <Map <string, Object> (); 19 Map <string, object> map1 = new hashmap <string, Object> (); 20 map1.put ("logo", R. drawable. add); 21 map1.put ("appname", "Beijing"); 22 list. add (map1); 23 24 Map <string, Object> MAP2 = new hashmap <string, Object> (); 25 map2.put ("logo", R. drawable. application); 26 map2.put ("appname", "Wuhan"); 27 List. add (MAP2); 28 return list; 29 30} 31 32}

Add data binding in the main program

 1 package com.example.androidspinner; 2  3 import java.util.List; 4 import java.util.Map; 5  6 import com.example.myadapter.Myadapter; 7  8 import android.os.Bundle; 9 import android.app.Activity;10 import android.view.Menu;11 import android.view.View;12 import android.widget.AdapterView;13 import android.widget.AdapterView.OnItemSelectedListener;14 import android.widget.ArrayAdapter;15 import android.widget.SimpleAdapter;16 import android.widget.Spinner;17 18 public class AndroidSpinnerActivity extends Activity {19         private Spinner spi2;20 21     @Override22     protected void onCreate(Bundle savedInstanceState) {23         super.onCreate(savedInstanceState);24         setContentView(R.layout.activity_android_spinner);25 26         spi2 = (Spinner) this.findViewById(R.id.spi2);27 28         List<Map<String, Object>> listMap = Myadapter.getMapData();29 30         SimpleAdapter simpleAdapter = new SimpleAdapter(31                 AndroidSpinnerActivity.this, listMap, R.layout.items,32                 new String[] { "logo", "appname" }, new int[] { R.id.imgview,33                         R.id.labview });34         spi2.setAdapter(simpleAdapter);35         36         spi2.setOnItemSelectedListener(new OnItemSelectedListener() {37 38             @Override39             public void onItemSelected(AdapterView<?> parent, View arg1,40                     int position, long id) {41                 String appname=((Map<String,Object>)spi2.getItemAtPosition(position)).get("appname").toString();42                 setTitle(appname);43             }44 45             @Override46             public void onNothingSelected(AdapterView<?> arg0) {47                 // TODO Auto-generated method stub48                 49             }50         });51     }52 53     @Override54     public boolean onCreateOptionsMenu(Menu menu) {55         // Inflate the menu; this adds items to the action bar if it is present.56         getMenuInflater().inflate(R.menu.activity_android_spinner, menu);57         return true;58     }59 60 }

Select a spinner from the list

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.