一.思路与效果图
思路:
ListView的应用
使用SimpAdapter创建ListVieiew
步骤:
1.使用ListView控件
2.创建条目(item)布局文件
3.数据(LIst<Map<String,Object>>类型)准备
4.创建SimpAdapter对象
5.ListView类型变量关联控件ListView控件
6.为ListView设置SimpAdapter
效果图:
二.右键新建ListView控件
*新建完成后的item_layout.xml代码块
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_item_stuNum"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="26sp"
android:layout_weight="3"
android:gravity="center"
/>
<TextView
android:id="@+id/tv_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="26sp"
android:layout_weight="2"
android:gravity="center"
/>
<TextView
android:id="@+id/tv_item_gender"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="26sp"
android:layout_weight="1"
android:gravity="center"
/>
</LinearLayout>
activity_main_XML代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_title_main_stuNum"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="学号"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/tv_title_main_name"
app:layout_constraintHorizontal_weight="3"
android:gravity="center"
/>
<TextView
android:id="@+id/tv_title_main_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="姓名"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/tv_title_main_stuNum"
app:layout_constraintRight_toLeftOf="@id/tv_title_main_gender"
app:layout_constraintHorizontal_weight="2"
android:gravity="center"
/>
<TextView
android:id="@+id/tv_title_main_gender"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="性别"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/tv_title_main_name"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_weight="1"
/>
<ListView
android:id="@+id/lv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/tv_title_main_gender"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
JAVA代码
package com.example.listview01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
// 5.ListView类型变量关联控件ListView控件
private ListView listView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 调用方法(5)
getViews();
// 学号
String[] stuNums=new String[]{
"202001-D-01","202001-D-02","202001-D-03","202001-D-04","202001-D-05"};
// 姓名
String[] stuNames=new String[]{
"小赵","小钱","小孙","小李","小王"};
// 性别
String[] genders=new String[]{
"男","男","男","女","女"};
// 3.数据(LIst<Map<String,Object>>类型)准备
List<Map<String,Object>> items=new ArrayList <Map<String,Object>>();
for (int i=0;i<stuNames.length;i++){
Map<String,Object> item=new HashMap<String,Object>();
item.put("stuNo",stuNums[i]);
item.put("name",stuNames[i]);
item.put("gender",genders[i]);
items.add(item);
}
// 5.ListView类型变量关联控件ListView控件
String []form=new String[]{
"stuNo","name","gender"};
int[]to=new int[]{
R.id.tv_item_stuNum,R.id.tv_item_name,R.id.tv_item_gender};
// 4.创建SimpAdapter对象
SimpleAdapter simpleAdapter=new SimpleAdapter(this,items,R.layout.item_layout,form,to);
// (this(内容上下文),items(集合类型的数据),R.layout.item_layout(条目布局文件资源),form(item标签名),to(控件ID数据));
// 6.为ListView设置SimpAdapter
listView.setAdapter(simpleAdapter);
}
// 5.ListView类型变量关联控件ListView控件-利用一个方法关联
private void getViews(){
listView=findViewById(R.id.lv_content);
}
}