android实现listview分页的方法
程序员文章站
2022-05-27 10:14:22
本文实例讲述了android实现listview分页的方法。分享给大家供大家参考。具体分析如下:
最近做了下listview的分页,跟web上的分页是一个意思,需要那几个...
本文实例讲述了android实现listview分页的方法。分享给大家供大家参考。具体分析如下:
最近做了下listview的分页,跟web上的分页是一个意思,需要那几个分页参数,不同的是sqlite中分页的查询语句,简便的方法需要用limit,offset关键字,前者是查询每页展示的记录数,后者是越过多少记录数,说得明白点就是忽略前面多少行记录之后,取多少行记录
我分页采用了一个重要的类page,通过封装page类,做为参数传递进来,返回出去也是个page对象
import java.util.collections; import java.util.list; /** * 注意所有序号从1开始. * * @param <t> page中记录的类型. * */ public class page<t> { //-- 公共变量 --// public static final string asc = "asc"; public static final string desc = "desc"; //-- 分页参数 --// protected int pageno = 0;// 当前页号<跟取数据的方式有关系> protected int pagesize = 1;// 每页显示的记录数 protected string orderby = null; protected string order = null; protected boolean autocount = true; //-- 返回结果 --// protected list<t> result = collections.emptylist(); protected long totalcount = -1;// 总记录数 //-- 构造函数 --// public page() { } public page(final int pagesize) { setpagesize(pagesize); } public page(final int pagesize, final boolean autocount) { setpagesize(pagesize); setautocount(autocount); } //-- 访问查询参数函数 --// /** * 获得当前页的页号,序号从0开始,默认为0. */ public int getpageno() { return pageno; } /** * 设置当前页的页号,序号从0开始,低于0时自动调整为0. */ public void setpageno(final int pageno) { this.pageno = pageno; if (pageno < 0) { this.pageno = 0; } } /** * 获得每页的记录数量,默认为1. */ public int getpagesize() { return pagesize; } /** * 设置每页的记录数量,低于0时自动调整为0. */ public void setpagesize(final int pagesize) { this.pagesize = pagesize; if (pagesize < 0) { this.pagesize = 0; } } /** * 根据pageno和pagesize计算当前页第一条记录在总结果集中的位置,序号从0开始. */ public int getfirst() { return (pageno * pagesize) + 1; } /** * 获得排序字段,无默认值.多个排序字段时用','分隔. */ public string getorderby() { return orderby; } /** * 设置排序字段,多个排序字段时用','分隔. */ public void setorderby(final string orderby) { this.orderby = orderby; } /** * 获得排序方向. */ public string getorder() { return order; } /** * 设置排序方式. * * @param order 可选值为desc或asc */ public void setorder(string order) { this.order = order; } /** * 查询对象时是否自动另外执行count查询获取总记录数, 默认为false. */ public boolean isautocount() { return autocount; } /** * 查询对象时是否自动另外执行count查询获取总记录数. */ public void setautocount(final boolean autocount) { this.autocount = autocount; } //-- 访问查询结果函数 --// /** * 取得页内的记录列表. */ public list<t> getresult() { return result; } /** * 设置页内的记录列表. */ public void setresult(final list<t> result) { this.result = result; } /** * 取得总记录数, 默认值为-1. */ public long gettotalcount() { return totalcount; } /** * 设置总记录数. */ public void settotalcount(final long totalcount) { this.totalcount = totalcount; } /** * 根据pagesize与totalcount计算总页数, 默认值为-1. */ public long gettotalpages() { if (totalcount < 0) return -1; long count = totalcount / pagesize; if (totalcount % pagesize > 0) { count++; } return count; } /** * 是否还有下一页. */ public boolean ishasnext() { return (pageno + 1 < gettotalpages()); } /** * 取得下页的页号, 序号从0开始. * 当前页为尾页时仍返回尾页序号. */ public int getnextpage() { if (ishasnext()) return pageno + 1; else return pageno; } /** * 是否还有上一页. */ public boolean ishaspre() { return (pageno - 1 >= 0); } /** * 取得上页的页号, 序号从1开始. * 当前页为首页时返回首页序号. */ public int getprepage() { if (ishaspre()) return pageno - 1; else return pageno; } }
下面是封装的pojo对象,界面listview的item展示就是靠它
public class record { private integer rid; private string fromuser; private string touser; private string content; private string rtime; private integer isreaded; public integer getrid() { return rid; } public void setrid(integer rid) { this.rid = rid; } public string getfromuser() { return fromuser; } public void setfromuser(string fromuser) { this.fromuser = fromuser; } public string gettouser() { return touser; } public void settouser(string touser) { this.touser = touser; } public string getcontent() { return content; } public void setcontent(string content) { this.content = content; } public string getrtime() { return rtime; } public void setrtime(string rtime) { this.rtime = rtime; } public integer getisreaded() { return isreaded; } public void setisreaded(integer isreaded) { this.isreaded = isreaded; } }
好吧,来看下dao类里的重要方法同样是page对象,在dao类里做的操作就是对总记录数和结果list进行赋值
public page<record> getrecordpage(page<record> page, string loginname, string contactname) { int pagesize = page.getpagesize(); int pageno = page.getpageno(); string sql = "select * from " + contactsmanagerdbadapter.tablename_record + " where (fromuser = ? and touser = ?) or (fromuser = ? and touser = ?)" + " limit " + string.valueof(pagesize) + " offset " + string.valueof(pageno * pagesize); string[] selectionargs = { loginname, contactname, contactname, loginname }; cursor cursor = msqlitedatabasewritable.rawquery(sql, selectionargs); list<record> result = new arraylist<record>(); for(cursor.movetofirst(); !cursor.isafterlast(); cursor.movetonext()){ record record = new record(); record.setrid(cursor.getint(0)); record.setfromuser(cursor.getstring(1)); record.settouser(cursor.getstring(2)); record.setcontent(cursor.getstring(3)); record.setrtime(cursor.getstring(4)); record.setisreaded(cursor.getint(5)); result.add(record); } page.settotalcount(getrowcount(loginname, contactname)); page.setresult(result); cursor.close(); return page; } /** * 总记录数 */ public int getrowcount(string loginname, string contactname){ string sql = "select * from " + contactsmanagerdbadapter.tablename_record + " where (fromuser = ? and touser = ?) or (fromuser = ? and touser = ?)"; string[] selectionargs = { loginname, contactname, contactname, loginname }; cursor cursor = msqlitedatabasewritable.rawquery(sql, selectionargs); int count = cursor.getcount(); cursor.close(); return count; }
关于listview的展示和自定义适配器,在此不提起,总之界面上上一页和下一页按钮的点击事件需要刷新listview
public class chathistory extends activity { listview historyview; button prevpagebtn; button nextpagebtn; textview historypagetv; button backchatshistorybtn; button deletehistorybtn; list<record> historyrecordlist; page<record> page; string loginname; string contactname; chathistoryservice chathistoryservice; contactsmanagerdbadapter dbadapter; private baseadapter adapter; private static final int status_change = 0; private handler mhandler; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.chats_history); historyview = (listview) findviewbyid(android.r.id.list); prevpagebtn = (button) findviewbyid(r.id.chat_prev_page); nextpagebtn = (button) findviewbyid(r.id.chat_next_page); historypagetv = (textview) findviewbyid(r.id.history_page); backchatshistorybtn = (button) findviewbyid(r.id.back_chats_history); deletehistorybtn = (button) findviewbyid(r.id.delete_history); sharedpreferences sharedata = chathistory.this.getsharedpreferences("data", 0); loginname = sharedata.getstring("loginname", ""); contactname = getintent().getstringextra("contactname"); dbadapter = new contactsmanagerdbadapter(getapplicationcontext()); dbadapter.open(); chathistoryservice = new chathistoryservice(chathistory.this); page = new page<record>(); page.setpagesize(8); page = chathistoryservice.gethistorypage(page, loginname, contactname, dbadapter); historyrecordlist = page.getresult(); updatetextview(); prevpagebtn.setonclicklistener(prevpagebuttonlistener); nextpagebtn.setonclicklistener(nextpagebuttonlistener); backchatshistorybtn.setonclicklistener(backbuttonlistener); deletehistorybtn.setonclicklistener(deletehistorybuttonlistener); adapter = new historylistadapter(chathistory.this); historyview.setadapter(adapter); mhandler = new handler(){ public void handlemessage(message msg) { switch(msg.what){ case status_change: // 处理ui更新等操作 updateui(); } }; }; } private void updateui() { //详细的更新 adapter.notifydatasetchanged();// 更新listview updatetextview(); } /** * 更新页码 */ private void updatetextview(){ if(historyrecordlist.size() == 0){ historypagetv.settext(0 + "/" + 0); } else{ historypagetv.settext(page.getpageno() + 1 + "/" + page.gettotalpages()); } } public class historylistadapter extends baseadapter{ private class recentviewholder { textview sender_context; textview rtime; } context context; layoutinflater minflater; public historylistadapter(context context){ this.context = context; minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); } @override public int getcount() { // todo auto-generated method stub return historyrecordlist.size(); } @override public object getitem(int position) { // todo auto-generated method stub return historyrecordlist.get(position); } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub recentviewholder holder; if (convertview == null) { convertview = minflater.inflate(r.layout.message_layout, null); holder = new recentviewholder(); holder.sender_context = (textview) convertview .findviewbyid(r.id.message_view_sender_content); holder.rtime = (textview) convertview .findviewbyid(r.id.message_view_timestamp); convertview.settag(holder); } else { holder = (recentviewholder) convertview.gettag(); } record record = historyrecordlist.get(position); if (record != null) { holder.sender_context.settext(record.getfromuser() + ":" + record.getcontent()); holder.rtime.settext(record.getrtime()); } return convertview; } } /** * 上一页按钮的监听事件 */ onclicklistener prevpagebuttonlistener = new onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub if(page.ishaspre()){ page.setpageno(page.getpageno() - 1); historyrecordlist = chathistoryservice.gethistorypage(page, loginname, contactname, dbadapter).getresult(); message msg = new message(); msg.what = status_change; mhandler.sendmessage(msg);// 向handler发送消息,更新ui } } }; /** * 下一页按钮的监听事件 */ onclicklistener nextpagebuttonlistener = new onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub if(page.ishasnext()){ page.setpageno(page.getpageno() + 1); historyrecordlist = chathistoryservice.gethistorypage(page, loginname, contactname, dbadapter).getresult(); message msg = new message(); msg.what = status_change; mhandler.sendmessage(msg);// 向handler发送消息,更新ui } } }; /** * 删除历史记录按钮监听器 */ onclicklistener deletehistorybuttonlistener = new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub } }; /** 退出聊天界面监听器 */ onclicklistener backbuttonlistener = new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub // 返回到聊天界面 finish(); dbadapter.close(); } }; @override public boolean onkeydown(int keycode, keyevent event) { // 按下键盘上返回按钮 if (keycode == keyevent.keycode_back) { finish(); dbadapter.close(); return true; } else { return super.onkeydown(keycode, event); } } }
上一页,下一页所做的操作也就是在判断是否有上一页和下一页的情况下对页号pageno的加减操作
最后写上chathistoryservice.java总觉得是多余的
public class chathistoryservice { context context; public chathistoryservice(context context) { this.context = context; } public page<record> gethistorypage(page<record> page, string loginname, string contactname, contactsmanagerdbadapter dbadapter) { recorddao recorddao = new recorddao(dbadapter); return recorddao.getrecordpage(page, loginname, contactname); } public int deletehistory(string loginname, string contactname, contactsmanagerdbadapter dbadapter){ recorddao recorddao = new recorddao(dbadapter); return recorddao.deletehistoryrecord(loginname, contactname); } }
顺带放上xml布局文件吧
chats_history.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 与好友聊天窗口.xml --> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white"> <listview android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="200dip" android:layout_weight="1" android:transcriptmode="normal" android:fadingedge="none" android:padding="4px" android:fastscrollenabled="true" android:smoothscrollbar="false" android:focusable="true" android:dividerheight="0dip" android:cachecolorhint="#00000000" android:divider="@drawable/divider" /> <!-- 引用聊天内容.xml --> <linearlayout android:layout_width="fill_parent" android:layout_height="50.0dip" android:orientation="horizontal" android:background="#ccc" android:paddingleft="3dip" android:paddingtop="3dip"> <button android:id="@+id/chat_prev_page" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/chat_prev_page" /> <textview android:id="@+id/history_page" android:layout_width="30.0dip" android:layout_height="25.0dip" android:gravity="center" android:text="1/1"/> <button android:id="@+id/chat_next_page" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/chat_next_page" /> <button android:id="@+id/delete_history" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/delete_history_button" /> <button android:id="@+id/export_history" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/export_history_button" /> <button android:id="@+id/back_chats_history" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/back_btn"/> </linearlayout> </linearlayout>
message_layout.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 消息内容的展示 --> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <!-- android:background="@drawable/item_style" --> <textview android:id="@+id/message_view_sender_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:autolink="all" /> <textview android:id="@+id/message_view_timestamp" android:layout_alignparentright="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/message_view_message" android:layout_gravity="right" /> </linearlayout>
最后来看看效果吧,别欺负我没贴上数据库建表语句,看下我的pojo类就知道我数据库表怎么建的
希望本文所述对大家的android程序设计有所帮助。
上一篇: 颜色变换 像字符逐字输入的欢迎词
下一篇: PS设计大苹果Macintosh标志
推荐阅读
-
Android控件Spinner的使用方法(1)
-
android 设置wallpaper的操作方法
-
Android定制RadioButton样式三种实现方法
-
Android实现简单时钟View的方法
-
android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍
-
android表格效果之ListView隔行变色实现代码
-
Android 实现带字母索引的侧边栏功能
-
Android开发之图形图像与动画(三)Animation效果的XML实现
-
Android实现TextView中文字链接的4种方式介绍及代码
-
Android开发之基于DialogFragment创建对话框的方法示例