ListView的使用

2024-06-14 12:04
文章标签 使用 listview

本文主要是介绍ListView的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

📖ListView的使用

    • ✅1. 创建ListView
    • ✅2. 创建适配器Adapter
    • ✅3. 开始渲染数据

主要3步骤:

  • 创建ListView

  • 创建适配器Adapter,和Adapter对应的视图

  • 开始渲染数据

效果图:

Snipaste_2024-06-12_23-50-43

✅1. 创建ListView

例如现有DemoActivity页面,对于布局文件activity_demo.xml

1.在activity_demo.xml中创建ListView组件

<?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="vertical"tools:context=".DemoActivity"><!--创建ListView--><ListViewandroid:id="@+id/lv_list"android:layout_width="match_parent"android:layout_height="wrap_content"android:divider="@null" /></LinearLayout>

2.在DemoActivity页面中获取组件

private static ListView lv_list;//全局声明protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_demo);lv_list = findViewById(R.id.lv_list);//获取视图组件}

✅2. 创建适配器Adapter

1.例如创建一个学生适配器StudentListAdapter,并继承BaseAdapter

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.st.bottomnavigation.R;
import com.st.bottomnavigation.bean.Student;import java.util.List;public class StudentListAdapter extends BaseAdapter {private Context mContext;//上下文private List<Student> studentList;//数据//构造方法public StudentListAdapter(Context mContext) {this.mContext = mContext;}//设置数据public void setData( List<Student> studentList){this.studentList = studentList;}//加载新的数据public void addData(List<Student> newData) {studentList.addAll(newData);}@Overridepublic int getCount() {return studentList.size();}@Overridepublic Object getItem(int i) {return studentList.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic View getView(int position, View convertView, ViewGroup viewGroup) {ViewHolder holder;// 1.推断是不是第一次进来if (convertView == null){convertView = LayoutInflater.from(mContext).inflate(R.layout.item_stu_listview,null);holder = new ViewHolder();holder.stuNo = convertView.findViewById(R.id.stuNo);  //holder.stuName = convertView.findViewById(R.id.stuName);  //convertView.setTag(holder);//标记,能够复用}else {holder = (ViewHolder) convertView.getTag();}//2.填充数据Student student = studentList.get(position);holder.stuNo.setText(student.getStuNo());  //学号holder.stuName.setText(student.getStuName());  //姓名//3.返回convertViewreturn convertView;}//创建ViewHolder,定义item_stu_listview.xml中的组件public final class ViewHolder {TextView stuNo;    //TextView stuName;  //}
}

2.创建实体类Student

public class Student {private String stuNo;private String stuName;public String getStuNo() {return stuNo;}public void setStuNo(String stuNo) {this.stuNo = stuNo;}public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}
}

3.创建对应视图文件item_stu_listview.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="wrap_content"android:background="#eeeeee"android:paddingTop="4dp"android:paddingBottom="4dp"android:paddingLeft="4dp"android:paddingRight="4dp"android:descendantFocusability="blocksDescendants"android:orientation="vertical"><LinearLayoutandroid:id="@+id/ll_item"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingBottom="10dp"android:paddingTop="10dp"android:paddingLeft="10dp"android:background="@drawable/shape_background_white"android:orientation="vertical"><!--第一行--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="学号:" /><TextViewandroid:id="@+id/stuNo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="" /></LinearLayout><!--第二行--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="姓名:" /><TextViewandroid:id="@+id/stuName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="" /></LinearLayout></LinearLayout></LinearLayout>

对应的shape_background_white.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><solid android:color="#FFFFFFFF" /><strokeandroid:width="0dp"android:color="#999999" /><corners android:radius="10dp" /></shape>

✅3. 开始渲染数据

1.造数据,也可以直接从后端数据库获取的数据

   public List<Student> getData(){List<Student> list = new ArrayList<>();for (int i = 0; i < 10; i++) {Student student = new Student();student.setStuName("学生"+i);student.setStuNo(1000+i+"");list.add(student);}return list;}

2.封装渲染方法showStudentListView

    private void showStudentListView(List<Student> studentList) {if(studentListAdapter == null) {//第一次加载studentListAdapter = new StudentListAdapter(this);studentListAdapter.setData(studentList);lv_list.setAdapter(studentListAdapter);// 设置列表视图的适配器}else {// 非第一次,更新数据studentListAdapter.setData(studentList);studentListAdapter.notifyDataSetChanged();}}

3.调用并显示数据

List<Student> studentList = getData();//获取数据
showStudentListView(studentList);//显示数据

Snipaste_2024-06-12_23-50-43

这篇关于ListView的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1060353

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四