博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
转载 ListView动态更新数据
阅读量:5258 次
发布时间:2019-06-14

本文共 6324 字,大约阅读时间需要 21 分钟。

ListView就是可以显示一行行Item的控件,有时候数据非常多,通常需要分页显示,但为了减少用户的输入,我们可以动态更新ListView,把下一页要显示的数据的添加到当前ListView中。 先看看效果:     需要注意的是在什么时候去更新数据 ,listVIew的setOnScrollListener监听是否滚到了最后一条记录,     取到的数据加到list中,最后记得调用adapter的notifyDataSetChanged,通知listview改变。     不废话,上代码。     主文件:     view plain import java.util.ArrayList;  import java.util.List;  import android.app.Activity;  import android.os.AsyncTask;  import android.os.Bundle;  import android.widget.AbsListView;  import android.widget.AbsListView.OnScrollListener;  import android.widget.ListView;  import android.widget.Toast;  public class ListViewActivity extends Activity {  private ListView listView;  //定义适配器              private ListItemAdapter listadpter;  //定义每一页显示行数              private int VIEW_COUNT = 20;  //定义的页数              private int index = 0;  //当前页              private int currentPage = 1;  //所以数据的条数              private int totalCount;  //每次取的数据,只要最后一次可能不一样。              private int maxResult;  //泛型集合ArrayList              private ArrayList
arrayList = new ArrayList
(); //数据操作的dao类 StudentsDAO dao = new StudentsDAO(ListViewActivity.this); //实体bean Students students = new Students(); // 标记:上次的ID private boolean isUpdating = false; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //初始化界面 initView(); //插入100条记录。 dao.insert(); totalCount = dao.getCount(); maxResult = getMaxResult(); //调用dao里面的selectAll()方法 arrayList = dao.getAllItems(index, maxResult); //实例化适配器 System.out.println("arrlist-->"+arrayList.size()); listadpter = new ListItemAdapter(ListViewActivity.this,arrayList); //填充适配器 listView.setAdapter(listadpter); listView.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if(firstVisibleItem + visibleItemCount == totalItemCount && !isUpdating){ if(totalItemCount
> { @Override protected List
doInBackground(Void... params) { // TODO Auto-generated method stub isUpdating = true; index += VIEW_COUNT; List
list =new ArrayList
(); list = dao.getAllItems(index, maxResult); return list; } @Override protected void onPostExecute(List
result) { // TODO Auto-generated method stub super.onPostExecute(result); arrayList.addAll(result); listadpter.notifyDataSetChanged(); isUpdating = false; System.out.println("end update--------------"); } } private int getMaxResult(){ int totalPage = (totalCount+VIEW_COUNT-1)/VIEW_COUNT; return totalCount-(totalPage-1)*VIEW_COUNT; } } 数据库Dao文件: view plain package com.shao.list; import java.util.ArrayList; import java.util.Random; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class StudentsDAO extends SQLiteOpenHelper { private final static String AUDIODATABASE_NAME = "student.db"; private final static String TABLE = "student"; private final static String COLUM_ID = "id"; private final static String COLUM_NAME = "name"; private final static String COLUM_AGE = "age"; private final static int DATABASE_VERSION = 1; private long row; private ArrayList
arrayList; public StudentsDAO(Context context) { super(context, AUDIODATABASE_NAME, null, DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub String sql = "CREATE TABLE " + TABLE + " (" + COLUM_ID + " INTEGER primary key autoincrement, " + " " + COLUM_NAME + " text, " + " " + COLUM_AGE + " int)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub String sql = "DROP TABLE IF EXISTS " + TABLE; db.execSQL(sql); } public long insert() { SQLiteDatabase db = getWritableDatabase(); ContentValues cv = new ContentValues(); Random random = new Random(); for(int i=1;i<=100;i++) { cv.put(COLUM_ID, i); cv.put(COLUM_NAME, "name"+String.valueOf(i)); cv.put(COLUM_AGE, random.nextInt(100)); row = db.insert(TABLE, null, cv); } db.close(); return row; } //查询记录的总数 public int getCount() { SQLiteDatabase db = getWritableDatabase(); String sql = "select count(*) from '"+TABLE+"'"; Cursor c = db.rawQuery(sql, null); c.moveToFirst(); int length = c.getInt(0); c.close(); db.close(); // System.out.println("length-->"+length); return length; } public ArrayList
getAllItems(int firstResult, int maxResult) { arrayList=new ArrayList
(); SQLiteDatabase db = getWritableDatabase(); String sql = "select * from '"+TABLE+"' limit ?,?"; Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(firstResult), String.valueOf(maxResult)}); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); } for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { Students students=new Students(); students.setId(cursor.getInt(0)); students.setName(cursor.getString(1)); students.setAge(cursor.getInt(2)); arrayList.add(students); } cursor.close(); db.close(); return arrayList; } public ArrayList
selectAll() { arrayList=new ArrayList
(); SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.query(TABLE, null, null, null, null, null, null); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); } for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { Students students=new Students(); students.setId(cursor.getInt(0)); students.setName(cursor.getString(1)); students.setAge(cursor.getInt(2)); arrayList.add(students); } cursor.close(); db.close(); return arrayList; } }

转载于:https://www.cnblogs.com/xiao0/archive/2011/09/07/2170286.html

你可能感兴趣的文章
ServerSocket和Socket通信
查看>>
css & input type & search icon
查看>>
源代码的下载和编译读后感
查看>>
Kafka学习笔记
查看>>
Octotree Chrome安装与使用方法
查看>>
Windows 环境下基于 Redis 的 Celery 任务调度模块的实现
查看>>
趣谈Java变量的可见性问题
查看>>
C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
查看>>
ssm框架之将数据库的数据导入导出为excel文件
查看>>
语音识别中的MFCC的提取原理和MATLAB实现
查看>>
验证组件FluentValidation的使用示例
查看>>
0320-学习进度条
查看>>
解决windows系统的oracle数据库不能启动ora-00119和ora-00130的问题
查看>>
ip相关问题解答
查看>>
MetaWeblog API Test
查看>>
反弹SHELL
查看>>
关闭Chrome浏览器的自动更新和升级提示
查看>>
移动、尺寸改变
查看>>
poj2255Tree Recovery【二叉树重构】
查看>>
tcpcopy 流量复制工具
查看>>