Android紫薇软剑之Spinner之北上广(3)

发表于:2013-08-05来源:Csdn作者:lynnli_1229点击数: 标签:Spinner
list.add(map1); list.add(map2); list.add(map3); return list; } @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { switch (parent.getId()) { case R.id.spinner:

  list.add(map1);

  list.add(map2);

  list.add(map3);

  return list;

  }

  @Override

  public void onItemSelected(AdapterView parent, View view, int position, long id) {

  switch (parent.getId()) {

  case R.id.spinner:

  Toast.makeText(this, "selected city postion= " + position + ", id= " + id, Toast.LENGTH_LONG).show();

  break;

  case R.id.spinner2:

  Toast.makeText(this, "selected logo postion= " + position + ", id= " + id, Toast.LENGTH_LONG).show();

  break;

  }

  }

  @Override

  public void onNothingSelected(AdapterView parent) {

  }

  }

  package cn.lynn.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.View;

  import android.widget.AdapterView;

  import android.widget.AdapterView.OnItemSelectedListener;

  import android.widget.ArrayAdapter;

  import android.widget.SimpleAdapter;

  import android.widget.Spinner;

  import android.widget.Toast;

  /**

  * Spinner用于显示下拉选项列表,并支持对各选项进行选择

  * Spinner案例一:标准Spinner选择城市

  * Spinner案例二:自定义Spinner选择Logo

  * @author lynnli1229

  */

  public class SpinnerMainActivity extends Activity implements OnItemSelectedListener {

  // 标准样式的Spinner

  private Spinner normalSpinner;

  // 自定义样式的Spinner

  private Spinner customSpinner;

  private ArrayAdapter arrAdapter;

  private SimpleAdapter simpAdapter;

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  normalSpinner = (Spinner) findViewById(R.id.spinner);

  arrAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, getList());

  arrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

  normalSpinner.setAdapter(arrAdapter);

  // 设置选择下拉选项的监听

  normalSpinner.setOnItemSelectedListener(this);

  customSpinner = (Spinner) findViewById(R.id.spinner2);

  // 构造SimpleAdapter为customSpinner对象采集数据被并填充到相应的视图上

  // SimpleAdapter构造器的参数依次为:context上下文,填充数据List,

  // Adapter布局文件,用于填充数据的key值,用于显示数据的控件Id

  simpAdapter = new SimpleAdapter(this, getListMap(), R.layout.item,

  new String[] { "img", "txt" },

  new int[] { R.id.imageView, R.id.textView });

  customSpinner.setAdapter(simpAdapter);

  // 设置选择下拉选项的监听

  customSpinner.setOnItemSelectedListener(this);

  }

  /**

  * 采集List类型数据,用于填充ArrayAdapter

  */

  private List getList() {

  List list = new ArrayList();

  list.add("北京");

  list.add("上海");

  list.add("广州");

  return list;

  }

  /**

  * 采集List>类型数据,用于填充SimpleAdapter

  */

  private List> getListMap() {

  HashMap map1 = new HashMap();

  map1.put("img", R.drawable.item1);

  map1.put("txt", "adidas");

  HashMap map2 = new HashMap();

  map2.put("img", R.drawable.item2);

  map2.put("txt", "birds");

  HashMap map3 = new HashMap();

  map3.put("img", R.drawable.item3);

  map3.put("txt", "android");

  List> list = new ArrayList>();

  list.add(map1);

  list.add(map2);

  list.add(map3);

  return list;

  }

  @Override

  public void onItemSelected(AdapterView parent, View view, int position, long id) {

  switch (parent.getId()) {

  case R.id.spinner:

  Toast.makeText(this, "selected city postion= " + position + ", id= " + id, Toast.LENGTH_LONG).show();

  break;

  case R.id.spinner2:

  Toast.makeText(this, "selected logo postion= " + position + ", id= " + id, Toast.LENGTH_LONG).show();

原文转自:http://blog.csdn.net/creativemobile/article/details/9073819