<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/spinnerTest"
android:id="@+id/test"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/test"
android:layout_alignLeft="@id/test"
android:prompt="@string/citysList"
android:entries="@array/cityArray"
android:id="@+id/spinner"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/spinner"
android:layout_alignLeft="@id/spinner"
android:prompt="@string/citysList"
android:id="@+id/spinner1"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/spinner1"
android:layout_alignLeft="@id/spinner1"
android:prompt="@string/citysList"
android:id="@+id/spinner2"
/>
</RelativeLayout>
静态加载
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">下拉列表</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="citysList">城市列表</string>
<string name="spinnerTest">下拉列表测试</string>
<string-array name="cityArray">
<item> 长沙</item>
<item>北京</item>
<item> 上海</item>
<item> 天津</item>
</string-array>
</resources>
public class MainActivity extends Activity {
Spinner spinner1=null;
Spinner spinner2=null;
Spinner spinner3=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getComponent();
setSpinnerInfoByAdapter();
}
private void setSpinnerInfoByAdapter() {
//设置spinner2中的内容
String [] citys=new String []{"浏阳","广州","天津","长沙","上海","巴黎","北京"};
ArrayAdapter<String> adapter= new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, citys);
spinner2.setAdapter(adapter);
//设置spinner3中的内容
List<Map<String, Object>> cityList=getList();
String[] from={"pic","city"};
int[] to={R.id.cityImg, R.id.cityName};
SimpleAdapter simpleadapter=new SimpleAdapter(this, cityList, R.layout.spinner_liner, from, to);
spinner3.setAdapter(simpleadapter);
spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Spinner sp=(Spinner) parent;
Map<String , Object>map=(Map<String, Object>) sp.getSelectedItem();
LinearLayout liner=(LinearLayout) view;
TextView nameText=(TextView) liner.findViewById(R.id.cityName);
String name=nameText.toString();
System.out.println("view="+view);
System.out.println("position="+position);
System.out.println("id="+id);
System.out.println("id="+name);
Toast.makeText(MainActivity.this, "你选择了:"+map.get("city"), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
private void getComponent() {
spinner1=(Spinner) findViewById(R.id.spinner);
spinner2=(Spinner) findViewById(R.id.spinner1);
spinner3=(Spinner) findViewById(R.id.spinner2);
}
private List<Map<String, Object>> getList() {
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
Map<String, Object> city1=new HashMap<String, Object>();
city1.put("image", R.drawable.help2);
city1.put("city", "长沙");
list.add(city1);
Map<String, Object> city2=new HashMap<String, Object>();
city2.put("image", R.drawable.html);
city2.put("city", "郴州");
list.add(city2);
Map<String, Object> city3=new HashMap<String, Object>();
city3.put("image", R.drawable.html3);
city3.put("city", "娄底");
list.add(city3);
Map<String, Object> city4=new HashMap<String, Object>();
city4.put("image", R.drawable.iphone1);
city4.put("city", "株洲");
list.add(city4);
Map<String, Object> city5=new HashMap<String, Object>();
city5.put("image", R.drawable.java1);
city5.put("city", "常德");
list.add(city5);
Map<String, Object> city6=new HashMap<String, Object>();
city6.put("image", R.drawable.java3);
city6.put("city", "岳阳");
list.add(city6);
return list;
}
@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;
}
}