安卓通过spinner实现二级选择地区(根据思路可以写三级)

 思路I  :通过二维数据放我们需要的城市地区 ,第一个spinner加载第一级城市,监听第一级城市去控制第二个spinner加载二级城市。


demo下载地址http://download.csdn.net/detail/u012303938/8838489

 代码如下:

MainActivity

package com.example.testdiqu;

import java.util.ArrayList;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;


public class MainActivity extends ActionBarActivity {
	private Spinner parent,chaird;
	private ArrayList<String> list=new ArrayList<String>();//城市
	private ArrayList<String> clist=new ArrayList<String>();//子城市
    private String [][] citys=new String[][]{
    		{"济南市","历下区","市中区","槐荫区","天桥区","历城区","长清区","平阴县","济阳县","商河县","章丘市"},
    		{"青岛市","市南区","市北区","四方区","黄岛区","崂山区","李沧区","城阳区","胶州市","即墨市","平度市","胶南市","莱西市"},
    		{"淄博市","淄川区","张店区","博山区","临淄区","周村区","桓台县","高青县","沂源县"},
    		{"枣庄市","薛城区","峄城区","台儿庄区","山亭区","滕州市"},
    		{"东营市","东营区","河口区","垦利县","利津县","广饶县"},
    		{"潍坊市","潍城区","寒亭区","坊子区","奎文区","临朐县","昌乐县","青州市","诸城市","寿光市","安丘市","高密市","昌邑市"},
    		{"烟台市","芝罘区","福山区","牟平区","莱山区","长岛县","龙口市","莱阳市","莱州市","蓬莱市","招远市","栖霞市","海阳市"},
    		{"威海市","环翠区","文登市","荣成市","乳山市"},
    		{"济宁市","任城区","微山县","鱼台县","金乡县","嘉祥县","汶上县","泗水县","梁山县","曲阜市","兖州市","邹城市"},	
    		{"泰安市","泰山区","岱岳区","宁阳县","东平县","新泰市","肥城市"},
    		{"日照市","东港区","岚山区","五莲县","莒县"},
    		{"莱芜市","莱城区","钢城区"},
    		{"临沂市","兰山区","罗庄区","沂南县","郯城县","沂水县","苍山县","费县","平邑县"},
    		{"德州市","德城区","宁津县","庆云县","临邑县","齐河县","平原县","夏津县","武城县","乐陵市","禹城市","莒南县","蒙阴县","临沭县"},
    		{"聊城市","东昌府区","阳谷县","莘县","茌平县","东阿县","冠县","高唐县","临清市"},
    		{"滨州市","滨城区","惠民县","阳信县","无棣县","沾化县","博兴县","邹平县"},
    		{"菏泽市","牡丹区","曹县","单县","巨野县","定陶县","成武县","郓城县","鄄城县","东明县"}	
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        parent=(Spinner)findViewById(R.id.parent);
		chaird=(Spinner)findViewById(R.id.chaird);
		loadData();
    }
    private void loadData() {
		// TODO Auto-generated method stub
		for(int i=0;i<citys.length;i++){
			list.add(citys[i][0]);
			Log.i("test2",citys[i].length+"" );
		}
		//主城市
		ArrayAdapter<String> adapter=new ArrayAdapter<String>
		(MainActivity.this, android.R.layout.simple_spinner_item, list);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		parent.setAdapter(adapter);
		parent.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				    clist.clear();//清除数据
					for(int j=1;j<citys[position].length;j++){
						clist.add(citys[position][j]);
					}
					//次城市
					ArrayAdapter<String> adapter=new ArrayAdapter<String>
					(MainActivity.this, android.R.layout.simple_spinner_item, clist);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				chaird.setAdapter(adapter);
			}

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

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

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

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

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值