sqlite數據庫數據的分頁

sqlite數據庫數據的分頁android

 

效果圖示例sql

 

 

一、在清單裏添加對數據庫操做的權限數據庫

代碼ide

 

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>函數

 

=================================工具

 

二、有2個佈局 一個activity_main.xml 一個item_activity.xml佈局

1)activity_main.xml佈局文件this

 

代碼sqlite

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >xml

    <ListView
        android:id="@+id/listView_main_titlelist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true">
    </ListView>
<!-- 進度加載控件 -->
    <LinearLayout
        android:id="@+id/layout_more"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:textColor="#000"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:onClick="clickButton"
        android:visibility="invisible"
        android:gravity="center">
        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"/>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="點擊加載更多數據..."
            android:textSize="30sp"/>

    </LinearLayout>

</RelativeLayout>

 

-----------------------------------

item_activity.xml佈局文件

 

代碼

 

<?xml version="1.0" encoding="utf-8"?>
<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_item_listview_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="TextView"
        android:textColor="#f00"
        android:textSize="40sp" />

    <TextView
        android:id="@+id/text_item_listview_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="40sp" />

    <TextView
        android:id="@+id/text_item_listview_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="40sp" />

</LinearLayout>

 

--------------------------------

三、有2個類 一個工具類 用來對數據庫的增刪改查操做一個MainActivity類

 

工具類

 

代碼

 

public class Sqlite_operate_utils {

 private static String DB_PATH = Environment.getExternalStorageDirectory() + File.separator + "stuentinfo.db";
 private SQLiteDatabase db;
 
 //構造函數 new 該類的時候 就去找 須要找的 數據庫
 public Sqlite_operate_utils() {
  db = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
 }
 
 //查詢 數據的 方法1
 public Cursor sqlite_select(String content, String[] condition){
  return db.rawQuery(content, condition);
 }
 
 //查詢 數據 的 方法2
 public List<Map<String, String>> sqlite_selectlist(String content, String[] condition){
  Log.i("data", "cursor:");
  Cursor cursor = db.rawQuery(content, condition);
  return cursorToList(cursor);
 }
 //返回List
 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));
   }
   list.add(map);
  }
  cursor.close();
  Log.i("data", "list:" + list.size());
  return list;
 }
 
 //增刪改 的方法
 //返回布爾型 方便 查看 數據 操做 是否成功
 public boolean executeData(String execute_content, Object[] bindArgs){
  try {
   if(bindArgs == null){//要綁定佔位符 的參數值
    db.execSQL(execute_content);
    return true;
   }else{
    db.execSQL(execute_content, bindArgs);
    return true;
   }
  } catch (SQLException e) {
   e.printStackTrace();
   return false;
  }
 }
 
 //關閉db
 public void destroy(){
  if(db != null){
   db.close();
  }
 }
}

 

 

==========================

 

 

 

 

MianActivity類

 

代碼

 

 

public class MainActivity extends Activity {

 private ListView listview;
 private LinearLayout layout_progress;
 private Sqlite_operate_utils sql;
 private SimpleAdapter adapter;
 private List<Map<String, String>> list_datainfo;

 // 分頁屬性 聲明
 private int pageNo = 1;// 第幾頁
 private int pageSize = 10;// 每頁有多少條數據
 private int pageCount;// 總頁數
 private boolean isbottom;// 用來判斷 是不是 底部

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  this.listview = (ListView) this
    .findViewById(R.id.listView_main_titlelist);
  this.layout_progress = (LinearLayout) this
    .findViewById(R.id.layout_more);
  sql = new Sqlite_operate_utils();
  
  //計算 數據庫 學生信息表的總頁數
        String content = "select count(*) from studentinfo ";//一行一列 數據
  Cursor cursor = sql.sqlite_select(content, null);
  cursor.moveToFirst();//移動遊標 到一地個位置
     int count = cursor.getInt(0);//總記錄數
  //關閉遊標
  cursor.close();
  pageCount = (int)Math.ceil(count/(double)pageSize);
  
  list_datainfo = getInfo();
  adapter = new SimpleAdapter(this, list_datainfo, R.layout.item_listview,
    new String[] { "s_sex", "s_age", "s_score" }, new int[] {
      R.id.text_item_listview_name,
      R.id.text_item_listview_id,
      R.id.text_item_listview_score });
  listview.setAdapter(adapter);
  //listview滾動 事件 監聽
  listview.setOnScrollListener(new OnScrollListener() {
   @Override
   public void onScrollStateChanged(AbsListView view, int scrollState) {
    if(isbottom){
     if(pageCount == pageNo){
      Toast.makeText(MainActivity.this, "沒有更多數據!!", Toast.LENGTH_SHORT).show();
     }else{
      layout_progress.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(pageNo<pageCount){   pageNo++;   list_datainfo.addAll(getInfo());   //更新 listview 視圖   adapter.notifyDataSetChanged();  }  layout_progress.setVisibility(View.GONE); } private List<Map<String, String>> getInfo() {  List<Map<String, String>> list = new ArrayList<Map<String,String>>();  //分頁  int start = (pageNo - 1)*pageSize;  String content = "select * from studentinfo limit ?,?";  String[] condition = {start + "",pageSize + ""};  list = sql.sqlite_selectlist(content, condition);  return list; }}

相關文章
相關標籤/搜索