安卓作业----慕课移动互联网开发第五课作业spinner实现省市区三级联动

这是中国大学慕课移动终端应用开发的网课作业5,我会持续更新我的作业,如果有需要关注一下吧

这里我只能说完成了三级联动的效果,当然这并不是一个好的解决方法,但是已经满足作业要求。所以,仅供参考。

效果图

安卓作业----慕课移动互联网开发第五课作业spinner实现省市区三级联动_第1张图片

代码参考

布局文件:


<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:text="省市区三级联动"
        android:textSize="30dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        Spinner>

        <Spinner
            android:id="@+id/spinner2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        Spinner>

        <Spinner
            android:id="@+id/spinner3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        Spinner>

    LinearLayout>

LinearLayout>

逻辑代码:

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private Spinner mSpinner1,mSpinner2,mSpinner3;

    private ArrayList<String> sheng =  new ArrayList<>();
    private Map<String,ArrayList<String>> shengShiMap = new HashMap<>();
    private ArrayList<String> shi;
    private Map<String,ArrayList<String>> shiQuMap = new HashMap<>();
    private ArrayList<String> qu;
    private ArrayAdapter<String> mStringArrayAdapter1;
    private ArrayAdapter<String> mStringArrayAdapter2;
    private ArrayAdapter<String> mStringArrayAdapter3;

    private String strSheng = "";
    private String strShi = "";
    private String strqu = "";

    private void init(){
        mSpinner1 = findViewById(R.id.spinner1);
        mSpinner2 = findViewById(R.id.spinner2);
        mSpinner3 = findViewById(R.id.spinner3);
        
        sheng.add("江苏省");
        sheng.add("安徽省");
        //处理江苏省内的市
        shi = new ArrayList<>();
        shi.add("无锡市");
        shi.add("苏州市");
        shengShiMap.put("江苏省",shi);
        shi = null;
        //处理安徽省内的市
        shi = new ArrayList<>();
        shi.add("合肥市");
        shi.add("六安市");
        shengShiMap.put("安徽省",shi);
        shi = null;

        //处理无锡市
        qu = new ArrayList<>();
        qu.add("锡山区");
        qu.add("惠山区");
        qu.add("江阴市");
        shiQuMap.put("无锡市",qu);
        qu = null;

        //处理苏州市
        qu = new ArrayList<>();
        qu.add("虎丘区");
        qu.add("吴中区");
        shiQuMap.put("苏州市",qu);
        qu = null;

        //处理合肥市
        qu = new ArrayList<>();
        qu.add("肥西县");
        qu.add("肥东县");
        shiQuMap.put("合肥市",qu);
        qu = null;

        //处理六安市
        qu = new ArrayList<>();
        qu.add("金安区");
        qu.add("裕安区");
        shiQuMap.put("六安市",qu);
        qu = null;

        mStringArrayAdapter1 = new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,sheng);
        mSpinner1.setAdapter(mStringArrayAdapter1);

        mStringArrayAdapter2 = new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,shengShiMap.get("江苏省"));
        mSpinner2.setAdapter(mStringArrayAdapter2);

        mStringArrayAdapter3 = new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,shiQuMap.get("无锡市"));
        mSpinner3.setAdapter(mStringArrayAdapter3);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

        mSpinner1.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //获取选取的是哪个省
                String str1 = parent.getItemAtPosition(position).toString();

                mStringArrayAdapter2 = new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,shengShiMap.get(str1));
                mSpinner2.setAdapter(mStringArrayAdapter2);

                mStringArrayAdapter3 = new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,shiQuMap.get(shengShiMap.get(str1).get(0)));
                mSpinner3.setAdapter(mStringArrayAdapter3);


                strSheng = str1;
                strShi = shengShiMap.get(str1).get(0);
                strqu = shiQuMap.get(shengShiMap.get(str1).get(0)).get(0);

                String text = strSheng+strShi+strqu;
                Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
            }

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

            }
        });

        mSpinner2.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //获取某个市的值
                String str = parent.getItemAtPosition(position).toString();
                mStringArrayAdapter3 = new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,shiQuMap.get(str));
                mSpinner3.setAdapter(mStringArrayAdapter3);

                strShi = str;
                strqu = shiQuMap.get(str).get(0);

                String text = strSheng+strShi+strqu;
                Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
            }

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

            }
        });

        mSpinner3.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String str = parent.getItemAtPosition(position).toString();
                strqu = str;

                String text = strSheng+strShi+strqu;
                Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
            }

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

            }
        });
    }
}

你可能感兴趣的:(安卓学习笔记)