Android入门之简单的ListView和三级联动Spinner

简单的ListView

UI效果图

在这里插入图片描述

知识点

  1. ListView的ArrayAdapter数组内容插入
  2. 点击响应Toast

ui代码

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list1"
        ></ListView>

Java主体

public class MainActivity extends AppCompatActivity {
    private ListView list1;
    private ArrayAdapter<String>arr_adapter;
    private SimpleAdapter simple_adapter;

    private String[] strs = new String[] {
        "first", "second", "third", "fourth", "fifth","sixth","seventh","eighth"
    };//定义一个String数组用来显示ListView的内容
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list1 = findViewById(R.id.list1);
        arr_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strs);//导入strs数组
        list1.setAdapter(arr_adapter);
        //点击响应
        list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this,"你选中了第" + (i+1) +"行",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Spinner之简单的三级联动

效果图

在这里插入图片描述

知识点

  1. spinner的三级联动

UI代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">
//三个简单的spinner
    <Spinner
        android:id="@+id/s1"
        android:layout_width="120dp"
        android:layout_height="50dp"
        ></Spinner>
    <Spinner
        android:id="@+id/s2"
        android:layout_width="150dp"
        android:layout_height="50dp"
        ></Spinner>
    <Spinner
        android:id="@+id/s3"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        ></Spinner>
</LinearLayout>

Java主体

package com.example.spinnertry;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    private Spinner sp1, sp2, sp3;
    String[] str1 = new String[]{"请选择", "湖南省", "浙江省"};
    String[] str21 = new String[]{"请选择", "长沙市", "常德市", "湘潭市", "衡阳市", "岳阳市"};
    String[] str22 = new String[]{"请选择", "杭州市", "宁波市"};
    String[] str31 = new String[]{"请选择", "望城区", "岳麓区", "雨花区"};
    String[] str32 = new String[]{"请选择", "拱墅区", "江干区", "余杭区"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //控件绑定
        sp1 = findViewById(R.id.s1);
        sp2 = findViewById(R.id.s2);
        sp3 = findViewById(R.id.s3);
		//第一组省的字符串无条件导入
        ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, str1);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp1.setAdapter(adapter1);
		//以下是剩余字符串绑定
        final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,str21);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        final ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str22);
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        final ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str31);
        adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        final ArrayAdapter<String> adapter5 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str32);
        adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		//spinner省的点击响应
        sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, final int i1, long l) {
                if(i1 == 1)//选择了湖南
                    sp2.setAdapter(adapter2);//导入湖南省的城市
                else if(i1 == 2)//选择了浙江
                    sp2.setAdapter(adapter3);//导入浙江省的城市
                sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i2, long l) {
                        if(i2 == 1 && i1 == 1) {//选择了湖南的同时选择了长沙
                            sp3.setAdapter(adapter4);//导入长沙的区
                        }
                        else if(i2 == 1 && i1 == 2) {//选择了浙江的同时选择了杭州
                            sp3.setAdapter(adapter5);//导入杭州的区
                        }
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {
                    }
                });
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }
}