动态刷新ListView

2024-09-04 22:38
文章标签 动态 刷新 listview

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

要刷新ListView,只需要调用其适配器的notifyDataSetChanged()方法即可。


下面的例子是在“ListView的例子”的基础上演化而言,本文仅给出变化的部分。


布局文件:增加了一个按钮,动态增加一个学生记录。

<?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="vertical" ><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/label_for_listview"/><ListViewandroid:id="@+id/list_view"android:layout_width="fill_parent"android:layout_height="wrap_content"/><Button android:id="@+id/add_student"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/add_student"/></LinearLayout>

Java代码:对之前的代码进行了重构,增加了Button的消息处理,即增加一个学生记录,然后刷新ListView:

package com.example.hellolistview;import java.util.ArrayList;
import java.util.HashMap;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;public class MainActivity extends Activity {private int student_item_id = R.layout.student_item;private String[] columnNames = new String[] { "name", "score" };private int[] ids = new int[] { R.id.student_name, R.id.student_score };private ArrayList<HashMap<String, Object>> students = new ArrayList<HashMap<String, Object>>();private int student_id = 0;private Context context = null;BaseAdapter adapter = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_listview);context = this;student_id = 0;ListView listView = (ListView) this.findViewById(R.id.list_view);createStudents();//adapter = new SimpleAdapter(this, students, student_item_id, columnNames, ids);adapter = new AnotherAdapter();listView.setAdapter(adapter);Button addStudent = (Button) this.findViewById(R.id.add_student);addStudent.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {addStudent();adapter.notifyDataSetChanged();}});}private void createStudents() {for (int i = 1; i <= 10; i++) {addStudent();}}private void addStudent() {HashMap<String, Object> map = null;map = new HashMap<String, Object>();map.put(columnNames[0], "student-" + student_id);map.put(columnNames[1], String.valueOf(student_id * 10));student_id++;students.add(map);}public class AnotherAdapter extends BaseAdapter {@Overridepublic int getCount() {return students.size();}@Overridepublic Object getItem(int position) {return students.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) {convertView = LayoutInflater.from(context).inflate(R.layout.student_item, null);}TextView studentName = (TextView) convertView.findViewById(ids[0]);TextView studentScore = (TextView) convertView.findViewById(ids[1]);studentName.setText((CharSequence) students.get(position).get(columnNames[0]));studentScore.setText((CharSequence) students.get(position).get(columnNames[1]));return convertView;}}
}

运行效果:





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


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1137237

相关文章

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

springboot如何通过http动态操作xxl-job任务

《springboot如何通过http动态操作xxl-job任务》:本文主要介绍springboot如何通过http动态操作xxl-job任务的问题,具有很好的参考价值,希望对大家有所帮助,如有错... 目录springboot通过http动态操作xxl-job任务一、maven依赖二、配置文件三、xxl-

Java调用C#动态库的三种方法详解

《Java调用C#动态库的三种方法详解》在这个多语言编程的时代,Java和C#就像两位才华横溢的舞者,各自在不同的舞台上展现着独特的魅力,然而,当它们携手合作时,又会碰撞出怎样绚丽的火花呢?今天,我们... 目录方法1:C++/CLI搭建桥梁——Java ↔ C# 的“翻译官”步骤1:创建C#类库(.NET

MyBatis编写嵌套子查询的动态SQL实践详解

《MyBatis编写嵌套子查询的动态SQL实践详解》在Java生态中,MyBatis作为一款优秀的ORM框架,广泛应用于数据库操作,本文将深入探讨如何在MyBatis中编写嵌套子查询的动态SQL,并结... 目录一、Myhttp://www.chinasem.cnBATis动态SQL的核心优势1. 灵活性与可

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL

SpringCloud使用Nacos 配置中心实现配置自动刷新功能使用

《SpringCloud使用Nacos配置中心实现配置自动刷新功能使用》SpringCloud项目中使用Nacos作为配置中心可以方便开发及运维人员随时查看配置信息,及配置共享,并且Nacos支持配... 目录前言一、Nacos中集中配置方式?二、使用步骤1.使用$Value 注解2.使用@Configur

SpringBoot实现Kafka动态反序列化的完整代码

《SpringBoot实现Kafka动态反序列化的完整代码》在分布式系统中,Kafka作为高吞吐量的消息队列,常常需要处理来自不同主题(Topic)的异构数据,不同的业务场景可能要求对同一消费者组内的... 目录引言一、问题背景1.1 动态反序列化的需求1.2 常见问题二、动态反序列化的核心方案2.1 ht

golang实现动态路由的项目实践

《golang实现动态路由的项目实践》本文主要介绍了golang实现动态路由项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习... 目录一、动态路由1.结构体(数据库的定义)2.预加载preload3.添加关联的方法一、动态路由1

Python Selenium动态渲染页面和抓取的使用指南

《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配