Android初級教程對大量資料的做分頁處理理論知識

來源:互聯網
上載者:User

標籤:

有時候要載入的資料上千條時,頁面載入資料就會很慢(資料載入也屬於耗時操作)。因此就要考慮分頁甚至分批顯示。先介紹一些分頁的理論知識。對於具體用在哪裡,會在後續部落格中更新。分頁資訊

1,一共多少條資料

   select count(*) from blacktb;   效能低下         原因: sql解析器先查詢資料字典,把*轉成所有的列名和列的類型               然後把每行資料提取出來               最後統計多少行資料   select count(常量) from blacktb;   高效能的查詢             不需要每行的記錄,只需要行數

2,指定每頁顯示多少條

 需求制定每頁行數 如: 20

3,計算出共多少頁

  int pages = (int)Math.ceil(201 * 1.0 / 20); // 10.05

4,取每頁的資訊 3頁的資料

  select * from blacktb limit 取多少條 offset 從哪條資料;  select * from blacktb limit 行的起始位置 , 多少條資料;  如:取第3頁  PageNumber每頁顯示多少條資料   select * from blacktb limit (3 - 1)*PageNumber,PageNumber

一般資料在資料庫中取到顯示的。根據上述理論知識,下面給一些查詢資料庫的一些虛擬碼。在項目中使用分頁的時候,直接套用即可:
/** * @return 總資料個數 */public int getTotalRows() {SQLiteDatabase database = blackDB.getReadableDatabase();Cursor cursor = database.rawQuery("select count(1) from "+ BlackTable.BLACKTABLE, null);cursor.moveToNext();// 總行數int totalRows = cursor.getInt(0);cursor.close();// 關閉遊標return totalRows;}/** * @param currentPage *            當前頁的頁碼 * @param perPage *            每頁顯示多少條資料 * @return 當前頁的資料 */public List<BlackBean> getPageDatas(int currentPage, int perPage) {List<BlackBean> datas = new ArrayList<BlackBean>();SQLiteDatabase database = blackDB.getReadableDatabase();// 擷取blacktb的所有資料遊標 (2 + 3) + ""Cursor cursor = database.rawQuery("select " + BlackTable.PHONE + ","+ BlackTable.MODE + " from " + BlackTable.BLACKTABLE+ " limit ?,? ", new String[] {((currentPage - 1) * perPage) + "",perPage + ""});while (cursor.moveToNext()) {// 有資料,資料封裝BlackBean bean = new BlackBean();// 封裝黑名單號碼bean.setPhone(cursor.getString(0));// 封裝攔截模式bean.setMode(cursor.getInt(1));// 添加資料到集合中datas.add(bean);}cursor.close();// 關閉遊標database.close();// 關閉資料庫return datas;}/** * @param perPage *            指定每頁顯示多少條資料 * @return 總頁數 */public int getTotalPages(int perPage) {int totalRows = getTotalRows();// 計算出多少頁,採用ceil函數,返回不小於該數的最小整數 如 :6.1 返回7.0int totalPages = (int) Math.ceil(totalRows * 1.0 / perPage);return totalPages;}






Android初級教程對大量資料的做分頁處理理論知識

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.