Spinner下拉列表

learningITwell 2016-08-18

实现三个下拉按钮,要求选择第一个省份时,第二个城市,第三个景点同步跟踪相对应的省份
在资源文件夹的布局文件中XML代码:
<LinearLayout 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:orientation="vertical"
    tools:context="com.phone.day04_home02.MainActivity" >

    <Spinner 
        android:id="@+id/s1_sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        
        />
   
     <Spinner 
        android:id="@+id/s2_sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        />
      <Spinner 
        android:id="@+id/s3_sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
        

</LinearLayout>
在MainActivity中的代码,利用适配器完成实现
package com.phone.day04_home02;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {
	//创建适配器三个
	private ArrayAdapter<String> p_adapter,c_adapter,j_adapter;
	//创建对应的资源数组,城市和景点采用二维数组
	private String[] province={"北京","上海","山东","江苏"};
	private String[][] city={{"海淀区","昌平区","朝阳区"},{"黄浦区","浦东新区"},{"济南","青岛","聊城"},{"苏州","南京","常州"}};
	private String[][] jingdian={{"故宫","颐和园","圆明园"},{"外滩","豫园","陆家嘴","南京步行街"},{"栈桥","大明湖","趵突泉"},{"苏州园林","南京长江大桥","江南小镇"}};
	//声明Spinner的引用
	private Spinner s1_sp;
	private Spinner s2_sp;
	private Spinner s3_sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		s1_sp=(Spinner) findViewById(R.id.s1_sp);
		s2_sp=(Spinner) findViewById(R.id.s2_sp);
		s3_sp=(Spinner) findViewById(R.id.s3_sp);
		//构建适配器
		p_adapter=new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item,province);
		c_adapter=new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item);
		c_adapter.addAll(city[0]);
		j_adapter=new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item);
		j_adapter.addAll(jingdian[0]);
		//将适配器和Spinner链接
		s1_sp.setAdapter(p_adapter);
		s2_sp.setAdapter(c_adapter);
		s3_sp.setAdapter(j_adapter);
		//设置省份Spinner的监听器
		s1_sp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				//清空适配器
			    c_adapter.clear();
				j_adapter.clear();
				//添加数组资源,跟组对应的位置
				c_adapter.addAll(city[position]);
				j_adapter.addAll(jingdian[position]);
				//Spinner默认选择重置为第一个
				s2_sp.setSelection(0);
				s3_sp.setSelection(0);
				
				
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

	@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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

相关推荐