关闭

多层下拉框

标签: 下拉框android
204人阅读 评论(0) 收藏 举报
分类:


package com.scxh;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
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;

public class MainActivity extends Activity {

	private ArrayList<String> city;
	private ArrayList<String> qu;
	private LayoutInflater inflater;
	private String[] sheng;
	private String[] sc_city;
	private String[] gd_city;
	private String[] cd_qu;
	private String[] zy_qu;
	private MyAdapter1 mMyAdapter1;
	private MyAdapter2 mMyAdapter2;
	private MyAdapter3 mMyAdapter3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sheng = getResources().getStringArray(R.array.sheng);
		sc_city = getResources().getStringArray(R.array.sc_city);
		gd_city = getResources().getStringArray(R.array.gd_city);
		cd_qu = getResources().getStringArray(R.array.cd_qu);
		zy_qu = getResources().getStringArray(R.array.zy_qu);
		city=new ArrayList<String>();
		qu=new ArrayList<String>();
		
		Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
		mMyAdapter1 = new MyAdapter1(this, -1);
		spinner1.setAdapter(mMyAdapter1);
		spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener1());
		
		Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
		mMyAdapter2 = new MyAdapter2(this, -1);
		spinner2.setAdapter(mMyAdapter2);
		spinner2.setOnItemSelectedListener(new MyOnItemSelectedListener2());
		
		Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
		mMyAdapter3 = new MyAdapter3(this, -1);
		spinner3.setAdapter(mMyAdapter3);
	
	}

	class MyAdapter1 extends ArrayAdapter<String> {

		public MyAdapter1(Context context, int resource) {
			super(context, resource);
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {

			return sheng.length;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.buju, null);
			}
			TextView textview = (TextView) convertView.findViewById(R.id.textview);
			textview.setText(sheng[position]);
			return convertView;
		}

		@Override
		public View getDropDownView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.buju, null);
			}
			TextView textview = (TextView) convertView.findViewById(R.id.textview);
			textview.setText(sheng[position]);
			return convertView;
		}

	}

	class MyAdapter2 extends ArrayAdapter<String> {

		public MyAdapter2(Context context, int resource) {
			super(context, resource);
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {

			return city.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.buju, null);
			}
			TextView textview = (TextView) convertView.findViewById(R.id.textview);
			textview.setText(city.get(position));
			return convertView;
		}

		@Override
		public View getDropDownView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.buju, null);
			}
			TextView textview = (TextView) convertView.findViewById(R.id.textview);
			textview.setText(city.get(position));
			return convertView;
		}

	}

	class MyAdapter3 extends ArrayAdapter<String> {

		public MyAdapter3(Context context, int resource) {
			super(context, resource);
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {

			return qu.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.buju, null);
			}
			TextView textview = (TextView) convertView.findViewById(R.id.textview);
			textview.setText(qu.get(position));
			return convertView;
		}

		@Override
		public View getDropDownView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.buju, null);
			}
			TextView textview = (TextView) convertView.findViewById(R.id.textview);
			textview.setText(qu.get(position));
			return convertView;
		}

	}

	class MyOnItemSelectedListener1 implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			switch (position) {
			case 0:
				city.clear();
				for (String i : sc_city) {
					city.add(i);
				}
				mMyAdapter2.notifyDataSetChanged();
				break;
				
			case 1:
				city.clear();
				for (String i : gd_city) {
					city.add(i);
				}
				mMyAdapter2.notifyDataSetChanged();
				break;

			default:
				break;
			}

		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {

		}
	}
	
	class MyOnItemSelectedListener2 implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			switch (position) {
			case 0:
				qu.clear();
				for (String i : cd_qu) {
					qu.add(i);
				}
				mMyAdapter3.notifyDataSetChanged();
				break;
			case 1:
				qu.clear();
				for (String i : zy_qu) {
					qu.add(i);
				}
				mMyAdapter3.notifyDataSetChanged();
				break;

			default:
				break;
			}

		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {

		}
	}
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.scxh.MainActivity" >
    
    <Spinner android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />
    
    <Spinner android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />
    
    <Spinner android:id="@+id/spinner3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="sheng">
        <item >四川</item>
        <item >广东</item>
        <item >青海</item>
        <item >陕西</item>
    </string-array>
    
    <string-array name="sc_city">
        <item >成都</item>
        <item >资阳</item>
        <item >绵阳</item>
        <item >广元</item>
        <item >简阳</item>
    </string-array>
    
    <string-array name="gd_city">
        <item >广州</item>
        <item >深圳</item>
        <item >中山</item>
        <item >东莞</item>
    </string-array>
    
    <string-array name="cd_qu">
        <item >金牛</item>
        <item >成华</item>
        <item >高薪</item>
        <item >青羊</item>
        <item >武侯</item>
        <item >锦江</item>
    </string-array>
    
     <string-array name="zy_qu">
        <item >安岳</item>
        <item >乐至</item>
        <item >1</item>
        <item >2</item>
        <item >3</item>
    </string-array>
</resources>





0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20130次
    • 积分:1106
    • 等级:
    • 排名:千里之外
    • 原创:91篇
    • 转载:20篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论