// 用来存放动态生成的TextView
ArrayList<TextView> list= new ArrayList<TextView>();
// 获得数据库的记录总条数
totalCount = getRowNum();
Log.v("totalCount",totalCount+"");
// 以每页5条数据划分显示,计算共有几页
int page = (int)Math.ceil(totalCount/5.0);
Log.v("page",page+"");
// 根据页数创建TextView对象
for(int i=1;i<=page;i++){
TextView tv1 = new TextView(this);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(params);
tv1.setText(i+" ");
tv1.setGravity(Gravity.RIGHT);
// 把TextView对象添加进list
list.add(tv1);
layout.addView(tv1);
}
// 对TextView的点击事件进行响应
for(int j=0;j<list.size();j++){
list.get(j).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 进行分页读取数据,暂定每页5条数据
Log.d("text",((TextView)v).getText().toString());
contentData(Integer.parseInt(((TextView)v).getText().toString().trim())-1);
}
});
}
// 获取数据库里面总有的条数
public int getRowNum(){
DOSQLiteHelper SQLiteHelper = new DOSQLiteHelper(FirstSQLiteApp.this);
SQLiteDatabase Database = SQLiteHelper.getReadableDatabase();
Cursor cursor = Database.query("CLASS_DB", null, null, null, null, null, null);
return cursor.getCount(); }
//更新及显示列表
public void contentData(int Spage){
DOSQLiteHelper SQLiteHelper = new DOSQLiteHelper(FirstSQLiteApp.this);
SQLiteDatabase Database = SQLiteHelper.getReadableDatabase();
// LIMIT X OFFSET X*Y 是对数据库分页的重要语句,X=5是每次显示的数目条数,Y是表示第几页
Cursor cursor = Database.rawQuery("select * from CLASS_DB LIMIT 5 OFFSET "+5*Spage, null);
cursor.moveToFirst();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,new String[]{"NO","NAME"},new int[]{android.R.id.text1,android.R.id.text2});
setListAdapter(adapter);
Database.close(); }