您的位置:首页 > 移动开发 > Android开发

android 中spinner 三级联动

2016-01-10 16:43 411 查看
java代码如下

package com.zz.spinner;

import android.app.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.zz.spinner.bean.ThreeEntity;
import com.zz.spinner.bean.TwoEntity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener{
private Spinner oneSpinner;
private Spinner twoSpinner;
private Spinner threeSpinner;
private EditText editResult;

/**一级集合*/
private List<String> oneList = new ArrayList<String>();

/**二级集合*/
private List<TwoEntity> twoList = new ArrayList<TwoEntity>();

/**三级集合*/
private List<ThreeEntity> threeList = new ArrayList<ThreeEntity>();

/**默认集合*/
private List<String> indexList ;

/**适配器*/
private ArrayAdapter<String> adapter;

/**一级名称*/
private String oneName;

/**二级名称*/
private String twoName;

/**三级名称*/
private String threeName;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
// 初始化数据
adapterData(oneList,oneSpinner);
Log.i("zhouzhuo","日志打印了啊=====");

}

private void initData() {
///增加一级数据
oneList.add("山东");
oneList.add("河北");
oneList.add("河南");

//添加二级数据
TwoEntity two = new TwoEntity();

two.setTwoName("济南");
two.setParentName("山东");

TwoEntity two3 = new TwoEntity();

two3.setTwoName("青岛");
two3.setParentName("山东");

TwoEntity two4 = new TwoEntity();

two4.setTwoName("德州");
two4.setParentName("山东");

TwoEntity two1 = new TwoEntity();

two1.setTwoName("石家庄");
two1.setParentName("河北");

TwoEntity two5 = new TwoEntity();

two5.setTwoName("保定");
two5.setParentName("河北");

TwoEntity two2 = new TwoEntity();

two2.setTwoName("郑州");
two2.setParentName("河南");

twoList.add(two);
twoList.add(two1);
twoList.add(two2);
twoList.add(two3);
twoList.add(two4);
twoList.add(two5);

//添加三级数据
ThreeEntity three = new ThreeEntity();

three.setTwoName("章丘");
three.setParentName("济南");

ThreeEntity three1 = new ThreeEntity();

three1.setTwoName("城阳");
three1.setParentName("青岛");

ThreeEntity three2 = new ThreeEntity();
three2.setTwoName("齐河");
three2.setParentName("德州");

ThreeEntity three3 = new ThreeEntity();
three3.setTwoName("长安");
three3.setParentName("石家庄");

ThreeEntity three4 = new ThreeEntity();
three4.setTwoName("清苑");
three4.setParentName("保定");

ThreeEntity three5 = new ThreeEntity();
three5.setTwoName("登封");
three5.setParentName("郑州");

threeList.add(three);
threeList.add(three1);
threeList.add(three2);
threeList.add(three3);
threeList.add(three4);
threeList.add(three5);
}

private void initView() {
oneSpinner = (Spinner) findViewById(R.id.one_spinner);
twoSpinner = (Spinner) findViewById(R.id.two_spinner);
threeSpinner = (Spinner) findViewById(R.id.three_spinner);
oneSpinner.setOnItemSelectedListener(this);
twoSpinner.setOnItemSelectedListener(this);
threeSpinner.setOnItemSelectedListener(this);

editResult = (EditText) findViewById(R.id.display_edit);

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()){
case R.id.one_spinner://一级列表
Toast.makeText(this,"点击了一级列表",Toast.LENGTH_SHORT).show();
oneName = oneSpinner.getItemAtPosition(position).toString();
//  找到相应的二级数据
selectTwoData(oneName);

//适配二级数据
adapterData(indexList,twoSpinner);
break;
case R.id.two_spinner://二级列表
Toast.makeText(this,"点击了二级列表",Toast.LENGTH_SHORT).show();
twoName = twoSpinner.getItemAtPosition(position).toString();

//找到相应的三级数据
selectThreeData(twoName);

//适配三级数据
adapterData(indexList,threeSpinner);
break;
case R.id.three_spinner://  三级列表
threeName =threeSpinner.getItemAtPosition(position).toString();
editResult.setText(oneName+" "+twoName+" "+threeName);

break;

}
}

/***
* 找到相应的三级数据
* @param twoName
*/

private void selectThreeData(String twoName) {
indexList = new ArrayList<String>();
// 循环三级列表
for(int i=0;i<threeList.size();i++){
if(twoName.equals(threeList.get(i).getParentName())){
indexList.add(threeList.get(i).getTwoName());
}
}

}

private void adapterData(List<String> indexList, Spinner twoSpinner) {
Log.i("zhouzhuo","indexlist size:"+indexList.size());

adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,indexList);
//设置下拉列表风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将 adapter设置到spinner中
twoSpinner.setAdapter(adapter);
//twoSpinner.setVisibility(View.VISIBLE);

}

/**
*
* public void onNothingSelected(AdapterView<?> parent) {
oneSpinner.setSelection(0);
twoSpinner.setSelection(0);
threeSpinner.setSelection(0);
}
* @param parent
*/
@Override
public void onNothingSelected(AdapterView<?> parent) {
Log.i("zhouzhuo","onNothingSelected");
oneSpinner.setSelection(0);
twoSpinner.setSelection(0);
threeSpinner.setSelection(0);
}

/***
* 找到相应的二级数据
* @param oneName
*/
private void selectTwoData(String oneName) {
indexList = new ArrayList<String>();
//循环三级数据集合,找到符合条件的数据
for(int i=0;i<twoList.size();i++){
if(oneName.equals(twoList.get(i).getParentName())){
indexList.add(twoList.get(i).getTwoName());
}
}

}
}

xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="false"
>
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="20px"
android:stretchColumns="1" >

<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10px"
android:scrollbarAlwaysDrawVerticalTrack="false" >

<TextView
android:id="@+id/province_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="省份:" >
</TextView>

<Spinner
android:id="@+id/one_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Spinner>
</TableRow>

<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10px"
android:scrollbarAlwaysDrawVerticalTrack="false" >

<TextView
android:id="@+id/city_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="城市:" >
</TextView>

<Spinner
android:id="@+id/two_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Spinner>
</TableRow>

<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10px"
android:scrollbarAlwaysDrawVerticalTrack="false" >

<TextView
android:id="@+id/county_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="县城镇:" >
</TextView>

<Spinner
android:id="@+id/three_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Spinner>
</TableRow>

<EditText
android:id="@+id/display_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:hint="输出结果"
android:paddingTop="10px" >
</EditText>
</TableLayout>

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: