Android提高第八篇之SQLite分頁讀取

來源:互聯網
上載者:User

Android包含了常用於嵌入式系統的SQLite,免去了開發人員自己移植安裝的功夫。SQLite 支援多數 SQL92 標準,很多常用的SQL命令都能在SQLite上面使用,除此之外Android還提供了一系列自訂的方法去簡化對SQLite資料庫的操作。不過有跨平台需求的程式就建議使用標準的SQL語句,畢竟這樣容易在多個平台之間移植。

先貼出本文程式啟動並執行結果:

本文主要講解了SQLite的基本用法,如:建立資料庫,使用SQL命令查詢資料表、插入資料,關閉資料庫,以及使用GridView實現了一個分頁欄(關於GridView的用法),用於把資料分頁顯示。

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

view plaincopy to clipboardprint?
<?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>   
<?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的源碼如下:

 

view plaincopy to clipboardprint?
<?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="建立資料庫"></Button> 
    <Button android:layout_height="wrap_content" 
        android:layout_width="fill_parent" android:text="插入一串實驗資料" android:id="@+id/btnInsertRec"></Button> 
    <Button android:layout_height="wrap_content" android:id="@+id/btnClose" 
        android:text="關閉資料庫" 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> 
<?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="建立資料庫"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="插入一串實驗資料" android:id="@+id/btnInsertRec"></Button>
 <Button android:layout_height="wrap_content" android:id="@+id/btnClose"
  android:text="關閉資料庫" 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>
 

本文程式源碼如下:

view plaincopy to clipboardprint?
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;//顯示分頁資料    
    SQLiteDatabase db;    
    int id;//添加記錄時的id累加標記,必須全域    
    static final int PageSize=10;//分頁時,每頁的資料總數    
    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;// 分頁欄的資料來源,與PageSize和資料總數相關    
    
    @Override    
  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.