激情九九,六月婷婷七月丁香,天天五月天丁香婷婷深爱综合,国产色一区,国产一区二区三区免费在线观看,91最新网站

Android應(yīng)用之SQLite分頁(yè)讀取

來(lái)源:網(wǎng)絡(luò)

點(diǎn)擊:2064

A+ A-

所屬頻道:新聞中心

關(guān)鍵詞: Android,SQLite分頁(yè)

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

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

     

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

    分頁(yè)欄的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="創(chuàng)建數(shù)據(jù)庫(kù)"></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ù)庫(kù)" 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="創(chuàng)建數(shù)據(jù)庫(kù)"></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ù)庫(kù)" 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;//顯示分頁(yè)數(shù)據(jù)    
        SQLiteDatabase db;    
        int id;//添加記錄時(shí)的id累加標(biāo)記,必須全局    
        static final int PageSize=10;//分頁(yè)時(shí),每頁(yè)的數(shù)據(jù)總數(shù)    
        private static final String TABLE_NAME = "stu";    
        private static final String ID = "id";    
        private static final String NAME = "name";    
            
        SimpleAdapter saPageID;// 分頁(yè)欄適配器    
        ArrayList<HashMap<String, String>> lstPageID;// 分頁(yè)欄的數(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);//分頁(yè)欄控件    
            // 生成動(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, // 沒(méi)什么解釋    
                    lstPageID,// 數(shù)據(jù)來(lái)源    
                    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ù)所選分頁(yè)讀取對(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的分頁(yè)數(shù)據(jù)  
         * SQL:Select * From TABLE_NAME Limit 9 Offset 10;  
         * 表示從TABLE_NAME表獲取數(shù)據(jù),跳過(guò)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)前分頁(yè)的數(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);//顯示出來(lái)    
            rec.close();    
        }    
            
        /*  
         * 在內(nèi)存創(chuàng)建數(shù)據(jù)庫(kù)和數(shù)據(jù)表  
         */    
        void CreateDB() {    
            // 在內(nèi)存創(chuàng)建數(shù)據(jù)庫(kù)    
            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) {    
                }    
            }    
        }    
        
        /*  
         * 插入之后刷新分頁(yè)  
         */    
        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;//取得分頁(yè)數(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();    
        }    
    }   

    (審核編輯: 智匯小新)

    聲明:除特別說(shuō)明之外,新聞內(nèi)容及圖片均來(lái)自網(wǎng)絡(luò)及各大主流媒體。版權(quán)歸原作者所有。如認(rèn)為內(nèi)容侵權(quán),請(qǐng)聯(lián)系我們刪除。

    主站蜘蛛池模板: 久久精品a亚洲国产v高清不卡 | 精品日韩一区二区 | 99色视频在线观看 | 久久精品视频免费看 | 欧美日韩性生活视频 | 午夜120秒| 国产高清不卡一区二区三区 | 免费a级毛片 | 欧洲欧美成人免费大片 | 国产人成精品午夜在线观看 | 人人色在线视频播放 | 第一毛片 | 欧美高清在线 | 国产综合区 | 97色在线视频观看香蕉 | 国产人成77777视频网站 | 亚洲日韩欧洲无码av夜夜摸 | 免费在线观看一级毛片 | 日韩a在线播放 | 精品一区二区三区自拍图片区 | 欧美激情婷婷 | 猛男诞生记最新更新 | 久久精品视频免费 | 五月四房婷婷 | 色中色综合网 | 国产成人涩涩涩视频在线观看免费 | 伊人亚洲综合青草青草久热 | 久久这里只有精品久久 | 99精品久久久久中文字幕 | 手机看片国产免费 | 精品综合久久久久97 | 2019天堂精品视频在线观看 | 亚洲国产经典 | 久久久久久亚洲精品中文字幕 | 国产国语一级毛片 | 久久99热狠狠色一区二区 | 免费观看性行为的视频网站 | 婷婷六月激情 | 欧美色另类 | 青草视频.com| 99久久综合国产精品免费 |