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

相关文章

C语言中联合体union的使用

本文编辑整理自: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=179471 一、前言 “联合体”(union)与“结构体”(struct)有一些相似之处。但两者有本质上的不同。在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和。而在“联合”中,各成员共享一段内存空间, 一个联合变量

Tolua使用笔记(上)

目录   1.准备工作 2.运行例子 01.HelloWorld:在C#中,创建和销毁Lua虚拟机 和 简单调用。 02.ScriptsFromFile:在C#中,对一个lua文件的执行调用 03.CallLuaFunction:在C#中,对lua函数的操作 04.AccessingLuaVariables:在C#中,对lua变量的操作 05.LuaCoroutine:在Lua中,

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

Lipowerline5.0 雷达电力应用软件下载使用

1.配网数据处理分析 针对配网线路点云数据,优化了分类算法,支持杆塔、导线、交跨线、建筑物、地面点和其他线路的自动分类;一键生成危险点报告和交跨报告;还能生成点云数据采集航线和自主巡检航线。 获取软件安装包联系邮箱:2895356150@qq.com,资源源于网络,本介绍用于学习使用,如有侵权请您联系删除! 2.新增快速版,简洁易上手 支持快速版和专业版切换使用,快速版界面简洁,保留主

如何免费的去使用connectedpapers?

免费使用connectedpapers 1. 打开谷歌浏览器2. 按住ctrl+shift+N,进入无痕模式3. 不需要登录(也就是访客模式)4. 两次用完,关闭无痕模式(继续重复步骤 2 - 4) 1. 打开谷歌浏览器 2. 按住ctrl+shift+N,进入无痕模式 输入网址:https://www.connectedpapers.com/ 3. 不需要登录(也就是

Toolbar+DrawerLayout使用详情结合网络各大神

最近也想搞下toolbar+drawerlayout的使用。结合网络上各大神的杰作,我把大部分的内容效果都完成了遍。现在记录下各个功能效果的实现以及一些细节注意点。 这图弹出两个菜单内容都是仿QQ界面的选项。左边一个是drawerlayout的弹窗。右边是toolbar的popup弹窗。 开始实现步骤详情: 1.创建toolbar布局跟drawerlayout布局 <?xml vers

C#中,decimal类型使用

在Microsoft SQL Server中numeric类型,在C#中使用的时候,需要用decimal类型与其对应,不能使用int等类型。 SQL:numeric C#:decimal

探索Elastic Search:强大的开源搜索引擎,详解及使用

🎬 鸽芷咕:个人主页  🔥 个人专栏: 《C++干货基地》《粉丝福利》 ⛺️生活的理想,就是为了理想的生活! 引入 全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称 Elastic)是目前全文搜索引擎的首选,相信大家多多少少的都听说过它。它可以快速地储存、搜索和分析海量数据。就连维基百科、Stack Overflow、

flask 中使用 装饰器

因为要完成毕业设计,我用到fountain code做数据恢复。 于是在github上下载了fountain code的python原代码。 github上的作者用flask做了fountain code的demo。 flask是面向python的一个网站框架。 里面有用到装饰器。 今天笔试的时候,我也被问到了python的装饰器。

mathematica的使用

因为做实验用到Bloom filter这一技术,Bloom filter里面的数学公式可以用来画图。 那么用什么画图软件比较好呢? 当然是Mathematica啦。 利用代码Plot[{y=x},{x,0,100}] 就可以画出比较好的图 简直nice