Spinner开发三级联动

平时开发经常遇到开发选择 省份,城市和地区的 ,今天抽空写了一个供大家快速开发

以下注意了,以下代码已测试可正常使用,因时间关系,初始数据就写了一点,大家在点击过程中如果出错,看看是不是选择了一个没有对应区的城市,会报空

FormActivity.java
package com.example.a20200712;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class FormActivity extends AppCompatActivity {
    private Spinner province;
    private Spinner city;
    private Spinner county;
    private TextView textView;

    private ArrayAdapter<OptionVO> provinceAdapter;
    private ArrayAdapter<OptionVO> cityAdapter;
    private ArrayAdapter<OptionVO> countyAdapter;

    private List<OptionVO> OpList;
    private int provincePosition = 0;
    private int cityPosition = 0;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.form_layout);
        province = findViewById(R.id.form_layout_province);
        city = findViewById(R.id.form_layout_city);
        county = findViewById(R.id.form_layout_county);
        textView = findViewById(R.id.form_layout_selected_county);
        init();


        provinceAdapter = new ArrayAdapter<OptionVO>(this, android.R.layout.simple_spinner_item, OpList);
        provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        province.setAdapter(provinceAdapter);
        province.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                cityAdapter = new ArrayAdapter<OptionVO>(getApplicationContext(), android.R.layout.simple_spinner_item, OpList.get(position).getChildren());
                cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                city.setAdapter(cityAdapter);
                provincePosition = position;

            }

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

            }
        });


        city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                countyAdapter = new ArrayAdapter<OptionVO>(getApplicationContext(), android.R.layout.simple_spinner_item, OpList.get(provincePosition).getChildren().get(position).getChildren());
                countyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                county.setAdapter(countyAdapter);
                cityPosition = position;
            }

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

            }
        });


        county.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                OptionVO vo = OpList.get(provincePosition).getChildren().get(cityPosition).getChildren().get(position);
                textView.setText(vo.getText() + ",值为" + vo.getValue());
            }

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

            }
        });

    }

    /**
     * 数据初始化,
     */
    private void init() {
        OpList = new ArrayList<OptionVO>();

        OptionVO op = new OptionVO("广东省", "1000");
        List<OptionVO> children = new ArrayList<OptionVO>();

        OptionVO gdszOP = new OptionVO("深圳", "1100");
        List<OptionVO> gdszChildren = new ArrayList<OptionVO>();
        gdszChildren.add(new OptionVO("罗湖区", "1110"));
        gdszChildren.add(new OptionVO("南山区", "1120"));
        gdszChildren.add(new OptionVO("福田区", "1130"));
        gdszOP.setChildren(gdszChildren);
        children.add(gdszOP);

        children.add(new OptionVO("广州", "1200"));
        children.add(new OptionVO("东莞", "1300"));
        op.setChildren(children);
        OpList.add(op);

        OptionVO op1 = new OptionVO("湖北省", "2000");
        List<OptionVO> children1 = new ArrayList<OptionVO>();

        OptionVO szOP = new OptionVO("随州", "2100");
        List<OptionVO> szChildren = new ArrayList<OptionVO>();
        szChildren.add(new OptionVO("曾都区", "2110"));
        szChildren.add(new OptionVO("广水", "2120"));
        szOP.setChildren(szChildren);
        children1.add(szOP);

        children1.add(new OptionVO("襄阳", "2200"));
        children1.add(new OptionVO("荆门", "2300"));
        op1.setChildren(children1);

        OpList.add(op1);

        OptionVO op2 = new OptionVO("江苏省", "3000");
        List<OptionVO> children2 = new ArrayList<OptionVO>();

        OptionVO njOP = new OptionVO("南京", "3100");
        List<OptionVO> njChildren = new ArrayList<OptionVO>();
        njChildren.add(new OptionVO("浦口区", "3110"));
        njChildren.add(new OptionVO("六合区", "3120"));
        njOP.setChildren(njChildren);
        children2.add(njOP);

        children2.add(new OptionVO("苏州", "3200"));
        children2.add(new OptionVO("盐城", "3300"));
        op2.setChildren(children2);
        OpList.add(op2);

    }
}

 

OptionVO.java 这个里面的 重写toString()非常重要,别忘记了
package com.example.a20200712;

import java.util.List;

public class OptionVO {
    private String text;
    private String value;
    private List<OptionVO> children;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public List<OptionVO> getChildren() {
        return children;
    }

    public void setChildren(List<OptionVO> children) {
        this.children = children;
    }

    public OptionVO(String text, String value) {
        this.text = text;
        this.value = value;
    }

    //重写toString()  因为适配器在显示数据的时候,如果传入适配器的对象不是字符串的情况下,直接就使用对象.toString()
    @Override
    public String toString() {
        return text;
    }

}

form_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
   >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="请选择您所在城市:"></TextView>
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">

      <Spinner
          android:id="@+id/form_layout_province"
          android:layout_width="match_parent"
          android:layout_weight="1"
          android:layout_height="wrap_content">
      </Spinner>

      <Spinner
          android:id="@+id/form_layout_city"
          android:layout_width="match_parent"
          android:layout_weight="1"
          android:layout_height="wrap_content">
      </Spinner>

      <Spinner
          android:id="@+id/form_layout_county"
          android:layout_width="match_parent"
          android:layout_weight="1"
          android:layout_height="wrap_content">
      </Spinner>

  </LinearLayout>

    <TextView
        android:id="@+id/form_layout_selected_county"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="您选择的城市是:"></TextView>
</LinearLayout>

 

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页