关闭

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

187人阅读 评论(0) 收藏 举报

效果图




  源代码为:

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>



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10938次
    • 积分:567
    • 等级:
    • 排名:千里之外
    • 原创:47篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条
    文章分类