1.只用XML配置来显示列表
在res\values中添加一个arrays.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string-array name="books"> 5 <item>口里奇才</item> 6 <item>凸显</item> 7 <item>下雨目前</item> 8 <item>越来越一直在于</item> 9 </string-array> 10 11 </resources>
在主页面的xml中添加Spinner
1 <Spinner 2 android:id="@+id/spi3" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:entries="@array/books" > 6 </Spinner>
效果
2.用后台动态添加
布局:
1 <Spinner 2 android:id="@+id/spi" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" > 5 </Spinner>
添加数据类
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 stub 14 } 15 16 public static List<String> getData(){ 17 List<String> list=new ArrayList<String>(); 18 list.add("北京"); 19 list.add("武汉"); 20 list.add("广州"); 21 return list; 22 } 23 24 }
主程序中添加数据绑定
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 @Override 22 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 @Override 43 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.选项加图标
布局:
1 <Spinner 2 android:id="@+id/spi2" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" > 5 </Spinner>
添加数据类
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 stub 14 } 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", "北京"); 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", "武汉"); 27 list.add(map2); 28 return list; 29 30 } 31 32 }
主程序中添加数据绑定
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 @Override 22 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 @Override 39 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 @Override 46 public void onNothingSelected(AdapterView<?> arg0) { 47 // TODO Auto-generated method stub 48 49 } 50 }); 51 } 52 53 @Override 54 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 }
本文转自欢醉博客园博客,原文链接http://www.cnblogs.com/zhangs1986/archive/2013/01/22/2871112.html如需转载请自行联系原作者
欢醉