21天学习android开发教程之SQLite分页读取,Android包含了常用于嵌入式系统的SQLite,免去了开发者自己移植安装的功夫,感兴趣的朋友可以参考一下
21天学习android开发教程之SQLite分页读取,Android包含了常用于嵌入式系统的SQLite,免去了开发者自己移植安装的功夫,感兴趣的朋友可以参考一下
21天学习android开发教程之SQLite分页读取,Android包含了常用于嵌入式系统的SQLite,免去了开发者自己移植安装的功夫,感兴趣的朋友可以参考一下
Android包含了常用于嵌入式系统的,免去了开发者自己移植安装的功夫。SQLite 支持多数 SQL92 标准,很多常用的SQL命令都能在SQLite上面使用,除此之外Android还提供了一系列自定义的方法去简化对SQLite数据库的操作。不过有跨平台需求的程序就建议使用标准的SQL语句,毕竟这样容易在多个平台之间移植。
本文主要讲解了SQLite的基本用法,如:创建数据库,使用SQL命令查询数据表、插入数据,关闭数据库,以及使用GridView实现了一个栏(关于GridView的用法),用于把数据分页显示。
分页栏的pagebuttons.xml的源码如下:
1
2
3
4
5
6
7
8
9
10
|
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns: = "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 = "创建数据库" ></ 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 > |
本文程序源码如下:
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; //显示分页数据 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 id="codetool">
以上就是本文的全部内容,希望对大家的学习有所帮助。 1 本网站名称:米米素材网
2 本站永久网址:https://www.mimisucai.cn 3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权进行删除处理。 4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。 5 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报 6 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。 相关阅读 |
发表评论