1、SQL数据库事务的使用(批量添加或删除使用)
db.beginTransaction();
/**
* 执行sql语句 db.execSQL(sql);
*/
db.setTransactionSuccessful();
db.endTransaction();
2、分页查询
思路就是计算显示多少页
1、先获取数据库中的总条目
/**
* 获取数据的总条目
* @param type 根据类型去查询数据
* @return 总条目
*/
public int getDataCount(String type) {
int count = 0;
db = helper.getWritableDatabase();
Cursor cursor = null;
if (type.equals("全部")) {
//查询全部表中数据的总条目
sql = "select * from account";
cursor = db.rawQuery(sql,null);
}else {
sql = "select * from account where type = ?";
cursor = db.rawQuery(sql, new String[]{type});
}
if (cursor != null) {
count = cursor.getCount(); //获取数据总条目
}
return count;
}
2、给定每页显示多少条数据‘
//accountSize表示的是每页显示的数目 我这里给定的是10
3、根据上面个条件算出总页码
//countSum是调用getDataCount得到的总条目,,accountNum总页数
accountNum = (int)Math.ceil(countSum/(double)accountSize); //根据总条目与每页展示数据条目 获得总页数
1、示例代码
private void initData() {
final DBDAO dbdao = new DBDAO(getContext()); //数据库操作类
if(type!=null) {
countSum = dbdao.getDataCount(type); //获取数据表的总条目
accountNum = (int)Math.ceil(countSum/(double)accountSize); //根据总条目与每页展示数据条目 获得总页数
if(currentAccount == 1){ //如果是第一页
//currentAccount表示的是页数,accountSize每一页显示多少条数据
mCostBeans = dbdao.selectCostBean(type,currentAccount,accountSize); //获取到数据源
}
final LsvAccountAdapter adapter = new LsvAccountAdapter(mCostBeans, getContext()); //列表适配器
lsvAccount.setAdapter(adapter);
//ListView滑动监听
lsvAccount.setOnScrollListener(new AbsListView.OnScrollListener() {
/**
*监听着ListView的滑动状态改变。官方的有三种状态SCROLL_STATE_TOUCH_SCROLL、
*SCROLL_STATE_FLING、SCROLL_STATE_IDLE:
* SCROLL_STATE_TOUCH_SCROLL:手指正拖着ListView滑动
* SCROLL_STATE_FLING:ListView正自由滑动
* SCROLL_STATE_IDLE:ListView滑动后静止
*/
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//isDivPage判断是否分页
if(isDivPage && AbsListView.OnScrollListener.SCROLL_STATE_IDLE == scrollState){
if(currentAccount < accountNum){
currentAccount ++;
//根据最新的页码加载获取集合到存储数据源中
mCostBeans.addAll(dbdao.selectCostBean(type,currentAccount,accountSize));
adapter.notifyDataSetChanged(); //刷新
}
}
}
/**
* firstVisibleItem: 表示在屏幕中第一条显示的数据在adapter中的位置
* visibleItemCount:则表示屏幕中最后一条数据在adapter中的数据,
* totalItemCount则是在adapter中的总条数
* */
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
isDivPage = ((firstVisibleItem + visibleItemCount) == totalItemCount);
}
});
}
``
selectCostBean代码
/**
* 根据当前页码查询获取该页码对应的数据
* @param type 查询的表数据类型
* @param currentAccount 当前页码
* @param accountSize 显示多少条数据
* @return 当前对应数据集合
*/
@Override
public ArrayList<CostBean> selectCostBean(String type, int currentAccount, int accountSize) {
ArrayList<CostBean> mBeans = new ArrayList<>();
CostBean bean = null;
int index = (currentAccount - 1) * accountSize; //获取当前页码第一条数据
db = helper.getWritableDatabase();
Cursor cursor;
if (!type.equals("全部")) {
sql = "select * from account where type = ? limit ?,?";
cursor = db.rawQuery(sql, new String[]{type, index + "", accountSize + ""});
} else {
sql = "select * from account limit ?,?";
cursor = db.rawQuery(sql, new String[]{index + "", accountSize + ""});
}
if (cursor != null) {
if (cursor.moveToLast()) { //是否最后一条记录
do {
bean = new CostBean();
bean.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
bean.setTime(cursor.getString(cursor.getColumnIndex("time")));
bean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
bean.setDescription(cursor.getString(cursor.getColumnIndex("description")));
bean.setType(cursor.getString(cursor.getColumnIndex("type")));
bean.setMoney(cursor.getFloat(cursor.getColumnIndex("money")));
bean.setImagePath(cursor.getString(cursor.getColumnIndex("imagePath")));
mBeans.add(bean);
} while (cursor.moveToPrevious()); //移动到上一条
cursor.close();
}
}
db.close();
return mBeans;
}