最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

21天學習android開發(fā)教程之SQLite分頁讀取

 更新時間:2016年02月10日 21:57:28   作者:aishu  
21天學習android開發(fā)教程之SQLite分頁讀取,Android包含了常用于嵌入式系統(tǒng)的SQLite,免去了開發(fā)者自己移植安裝的功夫,感興趣的朋友可以參考一下

Android包含了常用于嵌入式系統(tǒng)的SQLite,免去了開發(fā)者自己移植安裝的功夫。SQLite 支持多數(shù) SQL92 標準,很多常用的SQL命令都能在SQLite上面使用,除此之外Android還提供了一系列自定義的方法去簡化對SQLite數(shù)據(jù)庫的操作。不過有跨平臺需求的程序就建議使用標準的SQL語句,畢竟這樣容易在多個平臺之間移植。
本文主要講解了SQLite的基本用法,如:創(chuàng)建數(shù)據(jù)庫,使用SQL命令查詢數(shù)據(jù)表、插入數(shù)據(jù),關(guān)閉數(shù)據(jù)庫,以及使用GridView實現(xiàn)了一個分頁欄(關(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ù)據(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>

本文程序源碼如下:

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;//添加記錄時的id累加標記,必須全局 
  static final int PageSize=10;//分頁時,每頁的數(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);//分頁欄控件 
    // 生成動態(tài)數(shù)組,并且轉(zhuǎn)入數(shù)據(jù) 
    lstPageID = new ArrayList<HashMap<String, String>>(); 
 
    // 生成適配器的ImageItem <====> 動態(tài)數(shù)組的元素,兩者一一對應(yīng) 
    saPageID = new SimpleAdapter(testSQLite.this, // 沒什么解釋 
        lstPageID,// 數(shù)據(jù)來源 
        R.layout.pagebuttons,//XML實現(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ù)所選分頁讀取對應(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("當前分頁的數(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ù)中的每個字段 
      { 
        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(); 
  } 
} 

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。

相關(guān)文章

  • Android實現(xiàn)聊天記錄上傳本地服務(wù)器(即時通訊)

    Android實現(xiàn)聊天記錄上傳本地服務(wù)器(即時通訊)

    這篇文章主要為大家詳細介紹了Android實現(xiàn)聊天記錄上傳本地服務(wù)器,即時通訊功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標的解決方法

    Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標的解決方法

    這篇文章主要介紹了Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標的解決方法,涉及Android activity相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下
    2017-07-07
  • Android中Image的簡單實例詳解

    Android中Image的簡單實例詳解

    這篇文章主要為大家詳細介紹了Android中Image的簡單實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 如何為RecyclerView添加分隔線

    如何為RecyclerView添加分隔線

    這篇文章主要為大家詳細介紹了如何為RecyclerView添加分隔線,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android顯示系統(tǒng)SurfaceFlinger分析

    Android顯示系統(tǒng)SurfaceFlinger分析

    本文詳細講解了Android顯示系統(tǒng)SurfaceFlinger,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • Android Studio3.2中導出jar包的過程詳解

    Android Studio3.2中導出jar包的過程詳解

    這篇文章主要介紹了Android Studio3.2中導出jar包的過程,本文分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • 淺談android中數(shù)據(jù)庫的拷貝

    淺談android中數(shù)據(jù)庫的拷貝

    下面小編就為大家?guī)硪黄獪\談android中數(shù)據(jù)庫的拷貝。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Android上使用jspf插件框架的方法

    Android上使用jspf插件框架的方法

    這篇文章主要介紹了Android上使用jspf插件框架的方法,實例分析了jspf插件框架的功能與使用技巧,需要的朋友可以參考下
    2015-06-06
  • Listview的異步加載性能優(yōu)化

    Listview的異步加載性能優(yōu)化

    Android中ListView是使用平率最高的控件之一(GridView跟ListView是兄弟,都是繼承AbsListView),ListView優(yōu)化最有效的無非就是采用ViewHolder來減少頻繁的對view查詢和更新,緩存圖片加快解碼,減小圖片尺寸
    2016-01-01
  • Android中使用ListView模擬微信好友功能

    Android中使用ListView模擬微信好友功能

    這篇文章主要介紹了Android中使用ListView模擬微信好友功能,需要的朋友可以參考下
    2017-08-08

最新評論

温州市| 葫芦岛市| 抚顺县| 邵阳市| 东明县| 吉林市| 九龙县| 上思县| 泗洪县| 宝鸡市| 防城港市| 北京市| 洪雅县| 邯郸市| 鹤山市| 昌平区| 沛县| 格尔木市| 佛冈县| 临洮县| 昌平区| 无棣县| 成都市| 阿图什市| 潢川县| 阳春市| 富顺县| 大渡口区| 巴马| 临城县| 沭阳县| 三门县| 卢湾区| 广饶县| 汨罗市| 东明县| 安图县| 福建省| 饶平县| 将乐县| 井陉县|