package com.example.spinnerdemo;

 import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner; public class MainActivity extends Activity { private Spinner provinceSpinner = null; // 省(省、直辖市)
private Spinner citySpinner = null; // 市
private Spinner countrySpinner = null; // 区 private ArrayAdapter<String> provinceAdapter = null; // 省
private ArrayAdapter<String> cityAdapter = null; // 市
private ArrayAdapter<String> countryAdapter = null; // 区 private int provincePosition = 3; // 省级选项值
private String[] province = new String[] { "北京", "上海", "天津", "广东" };// ,"重庆","黑龙江","江苏","山东","浙江","香港","澳门"}; // 市级选项值
private String[][] city = new String[][] {
{ "东城区", "西城区", "崇文区", "宣武区", "朝阳区", "海淀区", "丰台区", "石景山区", "门头沟区","房山区", "通州区", "顺义区", "大兴区", "昌平区", "平谷区", "怀柔区", "密云县","延庆县" },
{ "长宁区", "静安区", "普陀区", "闸北区", "虹口区" },
{ "和平区", "河东区", "河西区", "南开区", "河北区", "红桥区", "塘沽区", "汉沽区", "大港区","东丽区" },
{ "广州", "深圳", "韶关" // ,"珠海","汕头","佛山","湛江","肇庆","江门","茂名","惠州","梅州",
// "汕尾","河源","阳江","清远","东莞","中山","潮州","揭阳","云浮"
} }; // 区县级选项值
private String[][][] country = new String[][][]
{
{ //北京
{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},
{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"}
},
{ //上海
{"无"},{"无"},{"无"},{"无"},{"无"}
},
{ //天津
{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"}
},
{ //广东
{"海珠区","荔湾区","越秀区","白云区","萝岗区","天河区","黄埔区","花都区","从化市","增城市","番禺区","南沙区"}, //广州
{"宝安区","福田区","龙岗区","罗湖区","南山区","盐田区"}, //深圳
{"武江区","浈江区","曲江区","乐昌市","南雄市","始兴县","仁化县","翁源县","新丰县","乳源县"} //韶关
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); provinceSpinner = (Spinner) findViewById(R.id.spinner_province);
citySpinner = (Spinner) findViewById(R.id.spinner_city);
countrySpinner = (Spinner) findViewById(R.id.spinner_country); // 装载适配器和值
provinceAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, province);
provinceSpinner.setAdapter(provinceAdapter); cityAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, city[3]);
citySpinner.setAdapter(cityAdapter); countryAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, country[3][0]);
countrySpinner.setAdapter(countryAdapter); // 省下拉框监听
provinceSpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) { cityAdapter = new ArrayAdapter<String>(
getApplication(),
android.R.layout.simple_spinner_item,
city[position]);
citySpinner.setAdapter(cityAdapter); provincePosition = position; // 记录当前省级序号,留给下面修改县级适配器时用
} @Override
public void onNothingSelected(AdapterView<?> arg0) { } }); // 市级下拉监听
citySpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
countryAdapter = new ArrayAdapter<String>(
getApplication(),
android.R.layout.simple_spinner_item,
country[provincePosition][position]);
countrySpinner.setAdapter(countryAdapter);
} @Override
public void onNothingSelected(AdapterView<?> arg0) { }
});
}
}

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:background="#e0e0e0"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spinner_province"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content" /> <Spinner
android:id="@+id/spinner_city"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content" /> <Spinner
android:id="@+id/spinner_country"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>

多层级Spinner列表选项实时更新树形层级(选择城市)的更多相关文章

  1. JComboBox组件的列表实时更新

    JComboBox deleteUser = new JComboBox(queryUser()); //queryUser()的返回值为字符串数组 deleteUser.setModel(new D ...

  2. facebook充值实时更新接口文档翻译 希望对做facebook充值的小伙伴有帮助

    Realtime Updates for Payments are an essential method by which you are informed of changes to orders ...

  3. 使用php+swoole对client数据实时更新(上)

    如果想对一个列表做实时的更新,传统的做法是采用轮询的方式.以web为例,通过Ajax定时请求服务端然后获取数据显示在页面.这种方式实现简单,缺点就是浪费资源. HTTP1.1新增加了对websocke ...

  4. 【C#】让DataGridView输入中实时更新数据源中的计算列

    本文适用Winform开发,且DataGridView的数据源为DataTable/DataView的情况. 理解前提:熟知DataTable.DataView 求:更好方案 考虑这样一个场景: 某D ...

  5. Web网站数据”实时”更新设计

    请注意这个实时打上了双引号,没有绝对的实时,只是时间的颗粒不一样罢了(1ms,1s,1m). 服务器数据有更新可以快速通知客户端.Web 基于取得模式,而服务器建立大量的和客户端连接来提供数据实时更新 ...

  6. iOS学习网站及大牛网址(实时更新)

    iOS学习网站及大牛网址(实时更新) 学习网站 https://github.com/Tim9Liu9/TimLiu-iOS  自己总结的iOS.mac开源项目及库 https://github.co ...

  7. python---django中form组件(2)自定制属性以及表单的各种验证,以及数据源的实时更新,以及和数据库关联使用ModelForm和元类

    自定义属性以及各种验证 分析widget: class TestForm(forms.Form): user = fields.CharField( required = True, widget = ...

  8. 解决 三星Note3 桌面小部件不实时更新/不刷新 的问题

    机型及问题描述:我的是三星note3 (国行 SM-N9008V),已ROOT,安装了LBE安全大师.在桌面小部件中,有些不会实时更新.比如有 滴答清单(办过的事项无法勾选),百度云音乐(歌曲播放更新 ...

  9. 实时更新Excel文档外部数据源的数据

    实时更新Excel文档外部数据源的数据 单元格区域.Excel 表.数据透视表或数据透视图均可以连接到外部数据源(数据源:用于连接数据库的一组存储的"源"信息.数据源包含数据库服务 ...

随机推荐

  1. poj 1860 Currency Exchange :bellman-ford

    点击打开链接 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16635   Accept ...

  2. [CF 475D] CGCDSSQ (RMQ)

    题目链接:http://codeforces.com/contest/475/problem/D 是昨天晚上的CF题目,题意是给定你n个数,问你所有子区间内的最小公约数是x的个数是多少 问的康神,了解 ...

  3. T4 assembly

    In a T4 template the executing assembly is not yours but one from the T4 engine. To access types fro ...

  4. 转载:Windows Phone 8.1 投影我的屏幕使用教程

    原文地址:http://livesino.net/archives/6851.live 更新了软件的下载地址. Windows Phone 8.1 新功能投影我的屏幕(Project My Scree ...

  5. DP Hrbust1186青蛙过河

    http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1186 #include<st ...

  6. 【CImg】三角形绘制算法实现

    这周的CV基础练习是简单的图形绘制:比如说矩形.三角形和圆心什么的.会发现其实矩形和圆形的实现思路都很直白,矩形只需要确认两个对角坐标就可以了,圆心只需要确认圆心和半径,接着就是简单的遍历各个像素点判 ...

  7. mysql显示乱码问题

    在select * from table:时往往会出现上图所示乱码现象 此时,输入status,会发现: 此时只要SET NAMES utf8即可解决该问题.此时,再次输入status:   总结:S ...

  8. sphinx 超好资料

    http://www.ttlsa.com/?s=sphinx

  9. 在Qt中使用AnyCAD三维建模控件

    AnyCAD C++ SDK专为Qt框架增加了AnyPlatformQt.lib模块,在Qt中使用AnyCAD三维建模控件变得十分简单. 下载 Qt高速下载:http://pan.baidu.com/ ...

  10. Delete PeopleSoft Query From the Database

    There could be different reasons why a PeopleSoft developer would like to delete a query from the da ...