public class MainActivity extends Activity { private ListView listview; private LinearLayout layout; private SimpleAdapter adapter; private MySQLiteDatabaseUtils databaseUtils; private boolean isBottom;// 判斷到達底部的標記 private List<Map<String, String>> toatlList; private int pageSize = 20;// 每頁能顯示的記錄數 private int pageIndex = 1;// 頁數。默認第一頁 private int pageCount;// 總頁數,記錄完了再也不加載數 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.listview = (ListView) this.findViewById(R.id.listview); this.layout = (LinearLayout) this.findViewById(R.id.layout); databaseUtils = new MySQLiteDatabaseUtils(); // 先計算總頁數 String sql = "select count(*)from studentinfo";// 一行一列數據,即一個表 Cursor cursor = databaseUtils.selectCursor(sql, null); cursor.moveToFirst();// 注意必定要移動遊標 int count = cursor.getInt(0);// 總記錄數,取列的總數 cursor.close(); // 以下:總頁數算法好比23.5,返回24,Math.floor(23.5),返回時23 pageCount = (int) Math.ceil(count / ((double) pageSize)); // 第一頁時,要向數據庫提取數據 if (pageIndex == 1) { toatlList = getSqlDataToListview(); } adapter = new SimpleAdapter(this, toatlList, R.layout.listview_item, new String[] { "sid", "sname", "sex", "score" }, new int[] { R.id.text_id, R.id.text_name, R.id.text_sex, R.id.text_score }); listview.setAdapter(adapter); listview.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (isBottom && scrollState == OnScrollListener.SCROLL_STATE_IDLE) { // 顯示底部的進度提示 layout.setVisibility(View.VISIBLE); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // 底部標記,每屏的第一個加最後一個等於每屏的總數 isBottom = ((firstVisibleItem + visibleItemCount) == totalItemCount); } }); } // 這個按鈕事件是到底部後,顯示出來,點擊就會加載數據 public void clickButton(View view) { if (pageIndex < pageCount) { pageIndex++; toatlList.addAll(getSqlDataToListview()); adapter.notifyDataSetChanged(); } else { Toast.makeText(this, "沒有數據要加載!!", 100).show(); } layout.setVisibility(View.INVISIBLE); } public List<Map<String, String>> getSqlDataToListview() { // 分頁Sql List<Map<String, String>> list = new ArrayList<Map<String, String>>(); String sql = "select * from studentinfo limit ?,?"; // 意思每次從start開始,取pageSize個記錄,將前一頁去過的數據去掉 int start = (pageIndex - 1) * pageSize; list = databaseUtils.selectList(sql, new String[] { start + "", pageSize + "" }); return list; } } //數據庫工具類 public class MySQLiteDatabaseUtils { private static final String dbPtah = Environment .getExternalStorageDirectory() + File.separator + "student.db"; private SQLiteDatabase db; /** * 創建數據庫鏈接的構造方法 */ public MySQLiteDatabaseUtils() { db = SQLiteDatabase.openDatabase(dbPtah, null, SQLiteDatabase.OPEN_READWRITE); } /** * 查詢方法一:返回的是遊標 * * @param sql * @param selectionArgs * @return */ public Cursor selectCursor(String sql, String[] selectionArgs) { Cursor cursor = db.rawQuery(sql, selectionArgs); return cursor; } /** * 查詢方法二:返回的是lsit集合 * * @param sql * @param selectionArgs * @return */ public List<Map<String, String>> selectList(String sql, String[] selectionArgs) { Cursor cursor = db.rawQuery(sql, selectionArgs); return cursorToList(cursor); } /** * 封裝遊標數據到List * * @param cursor * @return */ public List<Map<String, String>> cursorToList(Cursor cursor) { List<Map<String, String>> list = new ArrayList<Map<String, String>>(); while (cursor.moveToNext()) {// 循環的是每一行數據 Map<String, String> map = new HashMap<String, String>(); for (int i = 0; i < cursor.getColumnCount(); i++) {// 這個循環的是每一行的列數 map.put(cursor.getColumnName(i), cursor.getString(i));// 鍵:key:存儲每一行的字段,value:值就是內容 } list.add(map); } cursor.close(); return list; } /** * 增刪查的方法 * * @param sql * @param bindArgs * :是 sql語句中要綁定(佔位符)的參數值 * @return */ public boolean executeData(String sql, Object[] bindArgs) { try { if (bindArgs == null) { db.execSQL(sql); } else { db.execSQL(sql, bindArgs); } return true; } catch (SQLException e) { e.printStackTrace(); Log.i("MainActivity", "數據錯誤!!"); return false; } } // 關閉時銷燬db public void destroy() { if (db != null) { db.close(); } } } //主佈局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:background="#fff" android:gravity="center" android:onClick="clickButton" android:orientation="horizontal" android:textColor="#000" android:visibility="invisible" > <ProgressBar android:id="@+id/pb" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" /> <TextView android:id="@+id/textshow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:text="點擊加載更多數據" /> </LinearLayout> </RelativeLayout> //ListView的自定義佈局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text_id" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="TextView" android:textColor="#f00" android:textSize="30sp" /> <TextView android:id="@+id/text_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_weight="3" android:text="TextView" android:textColor="#00f" android:textSize="30sp" /> <TextView android:id="@+id/text_sex" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_weight="3" android:text="TextView" android:textColor="#00f" android:textSize="30sp" /> <TextView android:id="@+id/text_score" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_weight="3" android:text="TextView" android:textColor="#00f" android:textSize="30sp" /> </LinearLayout>