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>
转载于:https://my.oschina.net/u/2541146/blog/608646