一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Android - 21天學(xué)習(xí)android開發(fā)教程之SQLite分頁(yè)讀取

21天學(xué)習(xí)android開發(fā)教程之SQLite分頁(yè)讀取

2021-05-26 15:31aishu Android

21天學(xué)習(xí)android開發(fā)教程之SQLite分頁(yè)讀取,Android包含了常用于嵌入式系統(tǒng)的SQLite,免去了開發(fā)者自己移植安裝的功夫,感興趣的朋友可以參考一下

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

?
1
2
3
4
5
6
7
8
9
10
<?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的源碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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>

本文程序源碼如下:

?
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    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, // 沒什么解釋
            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ù)所選分頁(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ù),跳過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 id="codetool">

    以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

    延伸 · 閱讀

    精彩推薦
    主站蜘蛛池模板: 国产精品免费精品自在线观看 | 亚洲精品中文字幕久久久久久 | 午夜在线观看免费观看 视频 | 日本中文字幕在线视频站 | 日本高清二三四本2021 | naruto tube18动漫| 免费一级特黄特色大片 | 亚洲午夜性春猛交xxxx | 日韩精品一区二区三区中文字幕 | 免费精品在线视频 | 青青热久免费精品视频网站 | 亚洲精品久久久久69影院 | 欧美日韩亚洲高清不卡一区二区三区 | 欧美式禁忌 | 亚洲国产综合另类视频 | 国产区香蕉精品系列在线观看不卡 | 91精品国产91久久久久久麻豆 | 日韩欧美一区二区不卡 | 国产精品www视频免费看 | 国产日本久久久久久久久婷婷 | 男男同志gaysxxx | 免费十几分视频 | 91久久夜色精品国产九色 | 3344在线看片| 精品一区二区三区波多野结衣 | 风间由美理论片在线观看 | 亚洲精品一 | 成人a级特黄毛片 | 贵妇的私人性俱乐部 | 女黑人尺寸bbb | 免费观看国产大片资源视频 | 亚洲精品国产一区二区在线 | 3d欧美人与禽交 | 日本三不卡 | 国产欧美一区二区精品性色99 | 男生操女生的漫画 | 思思玖玖玖在线精品视频 | 精品欧美日韩一区二区三区 | 洗濯屋し在线观看 | 欧美精品99久久久久久人 | 日本偷拍xxxxxxww|
    <center id="qyiue"></center>
  • <input id="qyiue"><source id="qyiue"></source></input>
    <abbr id="qyiue"><menu id="qyiue"></menu></abbr>
  • <button id="qyiue"><noscript id="qyiue"></noscript></button>
        <input id="qyiue"><source id="qyiue"></source></input>
        <bdo id="qyiue"></bdo>