欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android提高之SQLite分頁讀取實(shí)現(xiàn)方法

 更新時(shí)間:2014年08月08日 17:48:22   投稿:shichen2014  
這篇文章主要介紹了Android的SQLite分頁讀取實(shí)現(xiàn)方法,在Android項(xiàng)目開發(fā)中非常實(shí)用,需要的朋友可以參考下

一般來說,Android自身就包含了常用于嵌入式系統(tǒng)的SQLite,這樣就免去了開發(fā)者自己移植安裝的功夫。SQLite 支持多數(shù)SQL92標(biāo)準(zhǔn),很多常用的SQL命令都能在SQLite上面使用,除此之外Android還提供了一系列自定義的方法去簡(jiǎn)化對(duì)SQLite數(shù)據(jù)庫的操作。不過有跨平臺(tái)需求的程序還是建議使用標(biāo)準(zhǔn)的SQL語句,畢竟這樣容易在多個(gè)平臺(tái)之間進(jìn)行移植。

先來貼出本文程序運(yùn)行的結(jié)果圖:

本文實(shí)例程序主要講解了SQLite的基本用法,如:創(chuàng)建數(shù)據(jù)庫,使用SQL命令查詢數(shù)據(jù)表、插入數(shù)據(jù),關(guān)閉數(shù)據(jù)庫,以及使用GridView實(shí)現(xiàn)了一個(gè)分頁欄(關(guān)于GridView的用法),用于把數(shù)據(jù)分頁顯示。

分頁欄的pagebuttons.xml的源碼如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="wrap_content" android:paddingBottom="4dip"
 android:layout_width="fill_parent">
 <TextView android:layout_width="wrap_content"
 android:layout_below="@+id/ItemImage" android:layout_height="wrap_content"
 android:text="TextView01" android:layout_centerHorizontal="true"
 android:id="@+id/ItemText">
 </TextView>
</RelativeLayout> 

main.xml的源碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <Button android:layout_height="wrap_content"
 android:layout_width="fill_parent" android:id="@+id/btnCreateDB"
 android:text="創(chuàng)建數(shù)據(jù)庫"></Button>
 <Button android:layout_height="wrap_content"
 android:layout_width="fill_parent" android:text="插入一串實(shí)驗(yàn)數(shù)據(jù)" android:id="@+id/btnInsertRec"></Button>
 <Button android:layout_height="wrap_content" android:id="@+id/btnClose"
 android:text="關(guān)閉數(shù)據(jù)庫" android:layout_width="fill_parent"></Button>
 <EditText android:text="@+id/EditText01" android:id="@+id/EditText01"
 android:layout_width="fill_parent" android:layout_height="256dip"></EditText>
 <GridView android:id="@+id/gridview" android:layout_width="fill_parent"
 android:layout_height="32dip" android:numColumns="auto_fit"
 android:columnWidth="40dip"></GridView>
</LinearLayout>

Java程序源碼如下:

package com.testSQLite; 
import java.util.ArrayList; 
import java.util.HashMap; 
import android.app.Activity; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.GridView; 
import android.widget.SimpleAdapter; 
public class testSQLite extends Activity { 
  /** Called when the activity is first created. */ 
  Button btnCreateDB, btnInsert, btnClose; 
  EditText edtSQL;//顯示分頁數(shù)據(jù) 
  SQLiteDatabase db; 
  int id;//添加記錄時(shí)的id累加標(biāo)記,必須全局 
  static final int PageSize=10;//分頁時(shí),每頁的數(shù)據(jù)總數(shù) 
  private static final String TABLE_NAME = "stu"; 
  private static final String ID = "id"; 
  private static final String NAME = "name"; 
  SimpleAdapter saPageID;// 分頁欄適配器 
  ArrayList<HashMap<String, String>> lstPageID;// 分頁欄的數(shù)據(jù)源,與PageSize和數(shù)據(jù)總數(shù)相關(guān) 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    btnCreateDB = (Button) this.findViewById(R.id.btnCreateDB); 
    btnCreateDB.setOnClickListener(new ClickEvent()); 
    btnInsert = (Button) this.findViewById(R.id.btnInsertRec); 
    btnInsert.setOnClickListener(new ClickEvent()); 
    btnClose = (Button) this.findViewById(R.id.btnClose); 
    btnClose.setOnClickListener(new ClickEvent()); 
    edtSQL=(EditText)this.findViewById(R.id.EditText01); 
    GridView gridview = (GridView) findViewById(R.id.gridview);//分頁欄控件 
    // 生成動(dòng)態(tài)數(shù)組,并且轉(zhuǎn)入數(shù)據(jù) 
    lstPageID = new ArrayList<HashMap<String, String>>(); 
    // 生成適配器的ImageItem <====> 動(dòng)態(tài)數(shù)組的元素,兩者一一對(duì)應(yīng) 
    saPageID = new SimpleAdapter(testSQLite.this, // 沒什么解釋 
        lstPageID,// 數(shù)據(jù)來源 
        R.layout.pagebuttons,//XML實(shí)現(xiàn) 
        new String[] { "ItemText" }, 
        new int[] { R.id.ItemText }); 
 
    // 添加并且顯示 
    gridview.setAdapter(saPageID); 
    // 添加消息處理 
    gridview.setOnItemClickListener(new OnItemClickListener(){ 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
          long arg3) { 
        LoadPage(arg2);//根據(jù)所選分頁讀取對(duì)應(yīng)的數(shù)據(jù) 
      } 
    }); 
  } 
  class ClickEvent implements View.OnClickListener { 
    @Override 
    public void onClick(View v) { 
      if (v == btnCreateDB) { 
        CreateDB(); 
      } else if (v == btnInsert) { 
        InsertRecord(16);//插入16條記錄 
        RefreshPage(); 
      }else if (v == btnClose) { 
        db.close(); 
      } 
    } 
  } 
  /* 
   * 讀取指定ID的分頁數(shù)據(jù) 
   * SQL:Select * From TABLE_NAME Limit 9 Offset 10; 
   * 表示從TABLE_NAME表獲取數(shù)據(jù),跳過10行,取9行 
   */ 
  void LoadPage(int pageID) 
  { 
    String sql= "select * from " + TABLE_NAME +  
    " Limit "+String.valueOf(PageSize)+ " Offset " +String.valueOf(pageID*PageSize); 
    Cursor rec = db.rawQuery(sql, null); 
    setTitle("當(dāng)前分頁的數(shù)據(jù)總數(shù):"+String.valueOf(rec.getCount())); 
    // 取得字段名稱 
    String title = ""; 
    int colCount = rec.getColumnCount(); 
    for (int i = 0; i < colCount; i++) 
      title = title + rec.getColumnName(i) + "   "; 
    // 列舉出所有數(shù)據(jù) 
    String content=""; 
    int recCount=rec.getCount(); 
    for (int i = 0; i < recCount; i++) {//定位到一條數(shù)據(jù) 
      rec.moveToPosition(i); 
      for(int ii=0;ii<colCount;ii++)//定位到一條數(shù)據(jù)中的每個(gè)字段 
      { 
        content=content+rec.getString(ii)+"   "; 
      } 
      content=content+"/r/n"; 
    } 
     
    edtSQL.setText(title+"/r/n"+content);//顯示出來 
    rec.close(); 
  } 
  /* 
   * 在內(nèi)存創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)表 
   */ 
  void CreateDB() { 
    // 在內(nèi)存創(chuàng)建數(shù)據(jù)庫 
    db = SQLiteDatabase.create(null); 
    Log.e("DB Path", db.getPath()); 
    String amount = String.valueOf(databaseList().length); 
    Log.e("DB amount", amount); 
    // 創(chuàng)建數(shù)據(jù)表 
    String sql = "CREATE TABLE " + TABLE_NAME + " (" + ID 
        + " text not null, " + NAME + " text not null " + ");"; 
    try { 
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
      db.execSQL(sql); 
    } catch (SQLException e) {} 
  } 
  /* 
   * 插入N條數(shù)據(jù) 
   */ 
  void InsertRecord(int n) { 
    int total = id + n; 
    for (; id < total; id++) { 
      String sql = "insert into " + TABLE_NAME + " (" + ID + ", " + NAME 
          + ") values('" + String.valueOf(id) + "', 'test');"; 
      try { 
        db.execSQL(sql); 
      } catch (SQLException e) { 
      } 
    } 
  } 
  /* 
   * 插入之后刷新分頁 
   */ 
  void RefreshPage() 
  { 
    String sql = "select count(*) from " + TABLE_NAME; 
    Cursor rec = db.rawQuery(sql, null); 
    rec.moveToLast(); 
    long recSize=rec.getLong(0);//取得總數(shù) 
    rec.close(); 
    int pageNum=(int)(recSize/PageSize) + 1;//取得分頁數(shù) 
     
    lstPageID.clear(); 
    for (int i = 0; i < pageNum; i++) { 
      HashMap<String, String> map = new HashMap<String, String>(); 
      map.put("ItemText", "No." + String.valueOf(i));
      lstPageID.add(map); 
    } 
    saPageID.notifyDataSetChanged(); 
  } 
}

感興趣的讀者可以動(dòng)手測(cè)試一下本實(shí)例代碼,希望能對(duì)大家進(jìn)行Android項(xiàng)目開發(fā)起到一定的參考借鑒作用。

相關(guān)文章

  • Android實(shí)現(xiàn)購物車整體頁面邏輯詳解

    Android實(shí)現(xiàn)購物車整體頁面邏輯詳解

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)購物車的整體頁面邏輯,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 使用IntelliJ IDEA 配置安卓(Android)開發(fā)環(huán)境的教程詳解(新手必看)

    使用IntelliJ IDEA 配置安卓(Android)開發(fā)環(huán)境的教程詳解(新手必看)

    這篇文章主要介紹了使用IntelliJ IDEA 配置安卓(Android)開發(fā)環(huán)境的教程詳解(新手必看),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • OKHttp使用詳解

    OKHttp使用詳解

    OkHttp 是一套處理 HTTP 網(wǎng)絡(luò)請(qǐng)求的依賴庫,由 Square 公司設(shè)計(jì)研發(fā)并開源,目前可以在 Java 和 Kotlin 中使用,這篇文章主要介紹了OKHttp詳解,需要的朋友可以參考下
    2024-01-01
  • 解析Android開發(fā)中多點(diǎn)觸摸的實(shí)現(xiàn)方法

    解析Android開發(fā)中多點(diǎn)觸摸的實(shí)現(xiàn)方法

    多點(diǎn)觸摸(MultiTouch),指的是允許計(jì)算機(jī)用戶同時(shí)通過多個(gè)手指來控制圖形界面的一種技術(shù)。與多點(diǎn)觸摸技術(shù)相對(duì)應(yīng)的就是單點(diǎn)觸摸,單點(diǎn)觸摸的設(shè)備已經(jīng)有很多年了,小尺寸的有觸摸式的手機(jī),大尺寸的最常見的就是銀行里的ATM機(jī)和排隊(duì)查詢機(jī)等等
    2013-05-05
  • Android界面數(shù)據(jù)懶加載實(shí)現(xiàn)代碼

    Android界面數(shù)據(jù)懶加載實(shí)現(xiàn)代碼

    這篇文章主要為大家分享了Android界面數(shù)據(jù)懶加載實(shí)現(xiàn)代碼,告訴大家怎樣實(shí)現(xiàn)界面即Fragment的懶加載,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android系統(tǒng)服務(wù)概覽

    Android系統(tǒng)服務(wù)概覽

    這篇文章介紹了Android系統(tǒng)服務(wù),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • 老項(xiàng)目遷移AndroidStudio3.0遇到的坑

    老項(xiàng)目遷移AndroidStudio3.0遇到的坑

    給大家分享了老的項(xiàng)目以及程序遷移到了AndroidStudio3.0遇到的坑和問題解決辦法,希望給你做個(gè)參考。
    2017-11-11
  • android雙緩沖技術(shù)實(shí)例詳解

    android雙緩沖技術(shù)實(shí)例詳解

    這篇文章主要介紹了android雙緩沖技術(shù)實(shí)例詳解,需要的朋友可以參考下
    2014-07-07
  • Android搜索框(SearchView)的功能和用法詳解

    Android搜索框(SearchView)的功能和用法詳解

    這篇文章主要為大家詳細(xì)介紹了Android搜索框SearchView的功能和用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android中ArrayList和數(shù)組相互轉(zhuǎn)換

    Android中ArrayList和數(shù)組相互轉(zhuǎn)換

    在我們?nèi)粘i_發(fā)中難免會(huì)要將ArrayList和數(shù)組相互轉(zhuǎn)換,那么如何才能相互轉(zhuǎn)換呢?下面跟著小編一起通過這篇文章學(xué)習(xí)學(xué)習(xí)。
    2016-08-08

最新評(píng)論