Sometimes when there are thousands of data to load, the page load data is slow (data loading is also a time-consuming operation). Therefore, it is necessary to consider paging or even batch display. First introduce some of the theoretical knowledge of paging. For specific use, it will be updated in subsequent blogs. Paging information
1, how many data?
select count(*) from blacktb; 性能低下 原因: sql解析器先查询数据字典,把*转成所有的列名和列的类型 然后把每行数据提取出来 最后统计多少行数据 select count(常量) from blacktb; 高性能的查询 不需要每行的记录,只需要行数
2, specify how many bars to display per page
需求制定每页行数 如: 20
3, calculate the total number of pages
int pages = (int)Math.ceil(201 * 1.0 / 20); // 10.05
4, take 3 pages of information per page of data
select * from blacktb limit 取多少条 offset 从哪条数据; select * from blacktb limit 行的起始位置 , 多少条数据; 如:取第3页 PageNumber每页显示多少条数据 select * from blacktb limit (3 - 1)*PageNumber,PageNumber
General data is displayed in the database. Based on the above theoretical knowledge, some pseudo-code for querying the database is given below. When using paging in a project, apply directly:
/** * @return Total data Count */public int gettotalrows () {sqlitedatabase database = Blackdb.getreadabledatabase (); cursor cursor = database.rawquery ("SELECT count (1) from" + blacktable.blacktable, NULL); Cursor.movetonext ();//Total Row int Totalrows = cursor.getint (0); Cursor.close ();//close cursor return totalrows;} /** * @param currentpage * The page number of the current page * @param perpage * How many data per page * @return data for the current page */public List<bla ckbean> getpagedatas (int currentpage, int perpage) {list<blackbean> datas = new arraylist<blackbean> (); Sqlitedatabase database = Blackdb.getreadabledatabase ();//Gets all data cursors for BLACKTB (2 + 3) + "cursor cursor = database.rawquery ( "Select" + Blacktable.phone + "," + Blacktable.mode + "from" + blacktable.blacktable+ "Limit?,?", new string[] {(cur RENTPAGE-1) * perpage) + "", Perpage + ""}), while (Cursor.movetonext ()) {//has data, data encapsulation blackbean bean = new Blackbean ();//seal Install the blacklist number Bean.setphone (cursor.getstring (0));//package interception mode Bean.setmode (Cursor.getint (1));//Add data to the collection Datas.add (Bean);} Cursor.close ();//close cursor database.close ();//close database return datas;} /** * @param perpage * Specify how many data to display per page * @return Total pages */public int gettotalpages (int perpage) {int totalrows = Getto Talrows ();//Calculate how many pages, using the Ceil function, return the smallest integer not less than the number such as: 6.1 returns 7.0int totalpages = (int) math.ceil (totalrows * 1.0/perpage); return T Otalpages;}
Android Beginner tutorial on a large amount of data doing paging processing theory knowledge