Android实现多层级Spinner列表选项实时更新树形层级

转载 2015年11月19日 09:38:51


package com.example.spinneradapter;  
  
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) {  
  
                    }  
                });  
    }  
}  

<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>  



相关文章推荐

Android实现多层级Spinner列表选项实时更新树形层级

Android实现多层级Spinner列表选项实时更新树形层级 运行结果如图: Java代码: package com.example.spinnerdemo; ...

多层级Spinner列表选项实时更新树形层级(选择城市)

package com.example.spinnerdemo; import android.os.Bundle; import android.app.Activity; import and...

Android自定义任意层级树形

  • 2015-11-23 11:09
  • 1.42MB
  • 下载

Android 打造任意层级树形控件 考验你的数据结构和设计

1、概述 大家在项目中或多或少的可能会见到,偶尔有的项目需要在APP上显示个树形控件,比如展示一个机构组织,最上面是boss,然后各种部门,各种小boss,最后各种小罗罗;整体是一个树形结构;遇...

Android 打造任意层级树形控件 考验你的数据结构和设计

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40212367,本文出自:【张鸿洋的博客】 1、概述 大家在项目中或多...

Android 打造任意层级树形控件 考验你的数据结构和设计

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40212367,本文出自:【张鸿洋的博客】 1、概述 大家在项目中或多...

Android 打造任意层级树形控件 考验你的数据结构和设计

转自:http://blog.csdn.net/lmj623565791/article/details/40212367,本文出自:【张鸿洋的博客】 1、概述 大家在项目中或多或少的可能...

Android 打造任意层级树形控件 考验你的数据结构和设计

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40212367,本文出自:【张鸿洋的博客】1、概述大家在项目中或多或少的可能会见到...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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