Android入门之简单的ListView和三级联动Spinner
简单的ListView
UI效果图

知识点
- ListView的ArrayAdapter数组内容插入
- 点击响应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"
};
@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);
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之简单的三级联动
效果图

知识点
- 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
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);
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) {
}
});
}
}