中文字幕视频在线免费_日韩在线精品_日韩视频免费看_中文字幕在线三区_午夜免费视频_日韩在线大片

Android應用之SQLite分頁讀取

來源:網絡

點擊:2087

A+ A-

所屬頻道:新聞中心

關鍵詞: Android,SQLite分頁

        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    
        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);//分頁欄控件    
            // 生成動態數組,并且轉入數據    
            lstPageID = new ArrayList<HashMap<String, String>>();    
        
            // 生成適配器的ImageItem <====> 動態數組的元素,兩者一一對應    
            saPageID = new SimpleAdapter(testSQLite.this, // 沒什么解釋    
                    lstPageID,// 數據來源    
                    R.layout.pagebuttons,//XML實現    
                    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);//根據所選分頁讀取對應的數據    
                }    
            });    
        
        }     
         


             
        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的分頁數據  
         * SQL:Select * From TABLE_NAME Limit 9 Offset 10;  
         * 表示從TABLE_NAME表獲取數據,跳過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("當前分頁的數據總數:"+String.valueOf(rec.getCount()));    
                
            // 取得字段名稱    
            String title = "";    
            int colCount = rec.getColumnCount();    
            for (int i = 0; i < colCount; i++)    
                title = title + rec.getColumnName(i) + "     ";    
        
                
            // 列舉出所有數據    
            String content="";    
            int recCount=rec.getCount();    
            for (int i = 0; i < recCount; i++) {//定位到一條數據    
                rec.moveToPosition(i);    
                for(int ii=0;ii<colCount;ii++)//定位到一條數據中的每個字段    
                {    
                    content=content+rec.getString(ii)+"     ";    
                }    
                content=content+"\r\n";    
            }    
                
            edtSQL.setText(title+"\r\n"+content);//顯示出來    
            rec.close();    
        }    
            
        /*  
         * 在內存創建數據庫和數據表  
         */    
        void CreateDB() {    
            // 在內存創建數據庫    
            db = SQLiteDatabase.create(null);    
            Log.e("DB Path", db.getPath());    
            String amount = String.valueOf(databaseList().length);    
            Log.e("DB amount", amount);    
            // 創建數據表    
            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條數據  
         */    
        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);//取得總數    
            rec.close();    
            int pageNum=(int)(recSize/PageSize) + 1;//取得分頁數    
                
            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();    
        }    
    }   

    (審核編輯: 智匯小新)

    聲明:除特別說明之外,新聞內容及圖片均來自網絡及各大主流媒體。版權歸原作者所有。如認為內容侵權,請聯系我們刪除。

    主站蜘蛛池模板: 久久久久一区 | 国产综合亚洲精品一区二 | 国产精品一区在线观看 | 中文日韩av | 国产高清视频一区 | 一本久久综合亚洲鲁鲁五月天 | 久久久久综合 | 亚洲综合欧美 | 国产三级在线 | 看特级毛片 | 久久99精品久久久久久久青青日本 | www.福利视频| 天天爽夜夜爽夜夜爽精品视频 | 亚洲日本国产 | 99这里只有精品 | 91精品欧美久久久久久动漫 | 91精品一区二区三区久久久久久 | 欧美色综合天天久久综合精品 | 国产一级久久久久 | 福利视频在线播放 | 国产精品久久久久久久久久久久冷 | 天天爽视频| 精品国产乱码久久久久久1区2区 | 午夜激情视频在线观看 | 久久综合久久综合久久综合 | 中文字幕在线观看视频一区 | 一级免费毛片 | 精品亚洲一区二区 | 国产成人综合网 | 伊人网综合 | 国产中文字幕在线免费观看 | 国产精品毛片 | 亚洲一区在线免费观看 | 亚洲精品综合 | 岛国一区 | 伊人久色 | 久久影音先锋 | 99精品欧美一区二区三区综合在线 | 久久久国产精品一区 | 欧美日本精品 | 91在线网站 |