Spinner下拉实现省市县同时跳转

原创 2015年11月19日 11:44:51

效果图




  源代码为:

package com.xiaoke.spinnershow;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	// 创建布局文件
	// 在资源文件values下面创建一个Android XML values File文件
	int temp;
	MyAdapter adapter2 = null;
	ArrayAdapter adapter3 = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 创建数据
		// String[] start={"北京","上海","成都"};
		// String[] terget={"北京","上海","成都"};
		// 获取资源文件中的数据
		final ArrayList<String[]> listShen = new ArrayList<String[]>();
		final ArrayList<String[]> listShi = new ArrayList<String[]>();
		final ArrayList<List<String[]>> listXian = new ArrayList<List<String[]>>();
		// 获取省
		final String[] city_start = this.getResources().getStringArray(
				R.array.city_start);
		listShen.add(city_start);
		// 获取市
		String[] city_shichuan = this.getResources().getStringArray(
				R.array.city_shichuan);
		String[] city_beijing = this.getResources().getStringArray(
				R.array.city_beijin);

		ArrayList<String[]> listShiCity_shichuan = new ArrayList<String[]>();
		listShi.add(city_shichuan);
		ArrayList<String[]> listShiCity_beijing = new ArrayList<String[]>();
		listShi.add(city_beijing);
		// 获取县
		String[] city_chengdu = this.getResources().getStringArray(
				R.array.city_chengdu);
		listShiCity_shichuan.add(city_chengdu);

		String[] city_miany = this.getResources().getStringArray(
				R.array.city_mianyang);
		listShiCity_shichuan.add(city_miany);

		String[] city_beijing_a = this.getResources().getStringArray(
				R.array.city_beijing_a);
		listShiCity_beijing.add(city_beijing_a);

		String[] city_beijing_b = this.getResources().getStringArray(
				R.array.city_beijing_b);
		listShiCity_beijing.add(city_beijing_b);

		listXian.add(listShiCity_shichuan);
		listXian.add(listShiCity_beijing);

		// 找到spinner对象
		Spinner sp1 = (Spinner) findViewById(R.id.spinner_a1_id);
		final Spinner sp2 = (Spinner) findViewById(R.id.spinner_a2_id);
		final Spinner sp3 = (Spinner) findViewById(R.id.spinner_a3_id);
		// 设置显示文件传递数据和绑定适配器sp1
		// ArrayAdapter adapter1=new ArrayAdapter(this,
		// android.R.layout.simple_list_item_1,android.R.id.text1, start);
		// sp1.setAdapter(adapter1);
		ArrayAdapter adapter1 = new MyAdapter(this, -1, listShen.get(0));
		sp1.setAdapter(adapter1);

		adapter2 = new MyAdapter(this, -1, listShi.get(0));
		sp2.setAdapter(adapter2);

		adapter3 = new MyAdapter(this, -1, listXian.get(0).get(0));
		sp3.setAdapter(adapter3);

		sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				temp = arg2;
				// 点击时跟新市的内容同时执行sp2的点击事件
				adapter2 = new MyAdapter(MainActivity.this, -1, listShi
						.get(arg2));
				sp2.setAdapter(adapter2);
			}

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

			}
		});
		sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// 更新县的内容
				adapter3 = new MyAdapter(MainActivity.this, -1, listXian.get(
						temp).get(arg2));
				sp3.setAdapter(adapter3);
			}

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

			}
		});

	}

	public class MyAdapter extends ArrayAdapter {
		LayoutInflater inflater = null;
		// 数据传递,获取外面的数据
		String[] list = null;

		public MyAdapter(Context context, int resource, String[] list) {
			super(context, resource, list);
			inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub

			return list.length;
		}

		// 界面显示的内容
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = inflater.inflate(android.R.layout.simple_list_item_1,
					null);
			TextView tv = (TextView) view.findViewById(android.R.id.text1);
			tv.setText(list[position]);
			return view;
		}

		// 下拉框里面的内容
		@Override
		public View getDropDownView(int position, View convertView,
				ViewGroup parent) {
			View view = inflater.inflate(android.R.layout.simple_list_item_1,
					null);
			TextView tv = (TextView) view.findViewById(android.R.id.text1);
			tv.setText(list[position] + "");
			return view;
		}

	}

}
布局文件代码

<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="horizontal" >

    <Spinner
        android:id="@+id/spinner_a1_id"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    <Spinner
        android:id="@+id/spinner_a2_id"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    <Spinner
        android:id="@+id/spinner_a3_id"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</LinearLayout>

资源文件代码

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="city_start">
        <item>四川</item>
        <item>北京</item>
    </string-array>
    <string-array name="city_shichuan">
        <item>成都</item>
        <item>绵阳</item>
    </string-array>
    <string-array name="city_beijin">
        <item>北京A</item>
        <item>北京B</item>
    </string-array>
    <string-array name="city_chengdu">
        <item>成都A</item>
        <item>成都B</item>
    </string-array>
    <string-array name="city_mianyang">
        <item>绵阳A</item>
        <item>绵阳B</item>
    </string-array>
    <string-array name="city_beijing_a">
        <item>北京A1</item>
        <item>北京A2</item>
    </string-array>
    <string-array name="city_beijing_b">
        <item>北京B1</item>
        <item>北京B2</item>
    </string-array>

</resources>



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Jquery多语言下拉控件,点击可实现跳转相应语言

$(document).ready(function () { //Define MultiLanguage var laugeYuan = ["zh_chs", "zh_tr", "...

使用Spinner实现下拉列表

  • 2016年03月21日 11:38
  • 994KB
  • 下载

玩转Android---UI篇---Spinner(下拉列表)

原址:http://hualang.iteye.com/category/143855 当在某个网站注册账号的时候,网站会让我们提供性别,生日,城市等信息,为了方便,就提供了一个下拉列表供我...

使用Spinner实现下拉列表

  • 2015年04月18日 11:58
  • 1.18MB
  • 下载

Android中使用Spinner实现下拉列表

  • 2017年04月25日 20:43
  • 27.14MB
  • 下载

Android中使用Spinner实现下拉列表

编写不易,如有转载,请声明出处: 梦回河口:1,Demo展示图片 2,布局代码//(layout)activity_main ...

Android_Spinner实现下拉选择菜单

XML文件:
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Spinner下拉实现省市县同时跳转
举报原因:
原因补充:

(最多只允许输入30个字)