多层下拉框

原创 2015年11月18日 18:45:25


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>





版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

下拉框三级联动

【Unity】I2本地化教程——下拉框选项

如何获得Popup List 的 Label的值 1.获得PopupList下拉框当前值使得调用其值 创建Sprite文件以及Label Sprite设置如下 Label设置如...

RepositoryItemComboBox获得焦点弹出下拉框

在dev中的gridcontrol中添加了一列  RepositoryItemComboBox下拉框,想要实现当  RepositoryItemComboBox列获得焦点的时候,自动弹出下拉框...

可以输入的下拉框

  • 2014-10-13 10:22
  • 113KB
  • 下载

带搜索功能的下拉框

  • 2017-08-21 14:50
  • 51KB
  • 下载

解决自定义标签select级联下拉框异步加载显示值不正确

解决自定义标签select级联下拉框异步加载显示值不正确
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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