Android提高第九篇之SQLite分页表格

上传人:yo****e 文档编号:59474489 上传时间:2022-03-03 格式:DOC 页数:13 大小:70.50KB
收藏 版权申诉 举报 下载
Android提高第九篇之SQLite分页表格_第1页
第1页 / 共13页
Android提高第九篇之SQLite分页表格_第2页
第2页 / 共13页
Android提高第九篇之SQLite分页表格_第3页
第3页 / 共13页
资源描述:

《Android提高第九篇之SQLite分页表格》由会员分享,可在线阅读,更多相关《Android提高第九篇之SQLite分页表格(13页珍藏版)》请在装配图网上搜索。

1、上次讲的Android上的SQLite分页读取,只用文本框显示数据而已,这次就讲得更加深入些,实现并封装一个SQL分页表格控件,不仅支持分页还是以表格的形式展示数据。先来看看本文程序运行的动画: 这个SQL分页表格控件主要分为“表格区”和“分页栏”这两部分,这两部分都是基于GridView实现的。网上介绍Android上实现表格的DEMO一般都用ListView。ListView与GridView对比,ListView最大的优势是格单元的大小可以自定义,可以某单元长某单元短,但是不能自适应数据表的结构;而GridView最大的优势就是自适应数据表的结构,但是格单元统一大小。对于数据表结构多变的

2、情况,建议使用GridView实现表格。本文实现的SQL分页表格控件有以下特点:1.自适应数据表结构,但是格单元统一大小;2.支持分页;3.“表格区”有按键事件回调处理,“分页栏”有分页切换事件回调处理。本文程序代码较多,可以到这里下载整个工程的源码:items.xml的代码如下,它是“表格区”和“分页栏”的格单元实现:view plaincopy to clipboardprint?01. 02. 06. 12. 13. main.xml的代码如下:view plaincopy to clipboardprint?01. 02. 05. 06. 09. 11. 13. 演示程序testSQL

3、ite.java的源码:view plaincopy to clipboardprint?01.package com.testSQLite; 02. 03.import android.app.Activity; 04.import android.database.Cursor; 05.import android.database.SQLException; 06.import android.database.sqlite.SQLiteDatabase; 07.import android.os.Bundle; 08.import android.util.Log; 09.import

4、 android.view.View; 10.import android.widget.Button; 11.import android.widget.LinearLayout; 12.import android.widget.Toast; 13. 14.public class testSQLite extends Activity 15. GVTable table; 16. Button btnCreateDB, btnInsert, btnClose; 17. SQLiteDatabase db; 18. int id;/添加记录时的id累加标记,必须全局 19. 20. pri

5、vate static final String TABLE_NAME = stu; 21. private static final String ID = id; 22. private static final String NAME = name; 23. private static final String PHONE = phone; 24. private static final String ADDRESS = address; 25. private static final String AGE = age; 26. 27. Override 28. public vo

6、id onCreate(Bundle savedInstanceState) 29. super.onCreate(savedInstanceState); 30. setContentView(R.layout.main); 31. btnCreateDB = (Button) this.findViewById(R.id.btnCreateDB); 32. btnCreateDB.setOnClickListener(new ClickEvent(); 33. 34. btnInsert = (Button) this.findViewById(R.id.btnInsertRec); 35

7、. btnInsert.setOnClickListener(new ClickEvent(); 36. 37. btnClose = (Button) this.findViewById(R.id.btnClose); 38. btnClose.setOnClickListener(new ClickEvent(); 39. 40. table=new GVTable(this); 41. table.gvSetTableRowCount(8);/设置每个分页的ROW总数 42. LinearLayout ly = (LinearLayout) findViewById(R.id.MainL

8、inearLayout); 43. 44. table.setTableOnClickListener(new GVTable.OnTableClickListener() 45. Override 46. public void onTableClickListener(int x,int y,Cursor c) 47. c.moveToPosition(y); 48. String str=c.getString(x)+ 位置:(+String.valueOf(x)+,+String.valueOf(y)+); 49. Toast.makeText(testSQLite.this, str

9、, 1000).show(); 50. 51. 52. ); 53. table.setOnPageSwitchListener(new GVTable.OnPageSwitchListener() 54. 55. Override 56. public void onPageSwitchListener(int pageID,int pageCount) 57. String str=共有+String.valueOf(pageCount)+ 58. 当前第+String.valueOf(pageID)+页; 59. Toast.makeText(testSQLite.this, str,

10、1000).show(); 60. 61. ); 62. 63. ly.addView(table); 64. 65. 66. class ClickEvent implements View.OnClickListener 67. 68. Override 69. public void onClick(View v) 70. if (v = btnCreateDB) 71. CreateDB(); 72. else if (v = btnInsert) 73. InsertRecord(16);/插入16条记录 74. 75. table.gvUpdatePageBar(select co

11、unt(*) from + TABLE_NAME,db); 76. table.gvReadyTable(select * from + TABLE_NAME,db); 77. else if (v = btnClose) 78. table.gvRemoveAll(); 79. db.close(); 80. 81. 82. 83. 84. 85. /* 86. * 在内存创建数据库和数据表 87. */ 88. void CreateDB() 89. / 在内存创建数据库 90. db = SQLiteDatabase.create(null); 91. Log.e(DB Path, db

12、.getPath(); 92. String amount = String.valueOf(databaseList().length); 93. Log.e(DB amount, amount); 94. / 创建数据表 95. String sql = CREATE TABLE + TABLE_NAME + ( + 96. ID + text not null, + NAME + text not null, + 97. ADDRESS + text not null, + PHONE + text not null, + 98. AGE + text not null +); 99.

13、try 100. db.execSQL(DROP TABLE IF EXISTS + TABLE_NAME); 101. db.execSQL(sql); 102. catch (SQLException e) 103. 104. 105. /* 106. * 插入N条数据 107. */ 108. void InsertRecord(int n) 109. int total = id + n; 110. for (; id total; id+) 111. String sql = insert into + TABLE_NAME + ( + 112. ID + , + NAME+, +

14、ADDRESS+, + PHONE+, +AGE 113. + ) values( + String.valueOf(id) + , man,address,123456789,18); 114. try 115. db.execSQL(sql); 116. catch (SQLException e) 117. 118. 119. 120. 121. 122. 123. package com.testSQLite;import android.app.Activity;import android.database.Cursor;import android.database.SQLExc

15、eption;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import android.widget.Toast;public class testSQLite extends Activity GVTable table;Button btnCreateDB, btnIns

16、ert, btnClose;SQLiteDatabase db;int id;/添加记录时的id累加标记,必须全局private static final String TABLE_NAME = stu;private static final String ID = id;private static final String NAME = name;private static final String PHONE = phone;private static final String ADDRESS = address;private static final String AGE =

17、age;Overridepublic 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.

18、setOnClickListener(new ClickEvent();btnClose = (Button) this.findViewById(R.id.btnClose);btnClose.setOnClickListener(new ClickEvent();table=new GVTable(this);table.gvSetTableRowCount(8);/设置每个分页的ROW总数LinearLayout ly = (LinearLayout) findViewById(R.id.MainLinearLayout);table.setTableOnClickListener(ne

19、w GVTable.OnTableClickListener() Overridepublic void onTableClickListener(int x,int y,Cursor c) c.moveToPosition(y);String str=c.getString(x)+ 位置:(+String.valueOf(x)+,+String.valueOf(y)+);Toast.makeText(testSQLite.this, str, 1000).show(););table.setOnPageSwitchListener(new GVTable.OnPageSwitchListen

20、er() Overridepublic void onPageSwitchListener(int pageID,int pageCount) String str=共有+String.valueOf(pageCount)+ 当前第+String.valueOf(pageID)+页;Toast.makeText(testSQLite.this, str, 1000).show(););ly.addView(table);class ClickEvent implements View.OnClickListener Overridepublic void onClick(View v) if

21、(v = btnCreateDB) CreateDB(); else if (v = btnInsert) InsertRecord(16);/插入16条记录table.gvUpdatePageBar(select count(*) from + TABLE_NAME,db);table.gvReadyTable(select * from + TABLE_NAME,db);else if (v = btnClose) table.gvRemoveAll();db.close();/* * 在内存创建数据库和数据表 */void CreateDB() / 在内存创建数据库db = SQLite

22、Database.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, + ADDRESS+ text not null, + PHONE + text not null, + AGE+ text not null +);t

23、ry 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+, + ADDRESS+, + PHONE+, +AGE+ ) values( + String.valueOf(id) + , man,addr

24、ess,123456789,18);try db.execSQL(sql); catch (SQLException e) 分页表格控件GVTable.java的源码:view plaincopy to clipboardprint?01.package com.testSQLite; 02. 03.import java.util.ArrayList; 04.import java.util.HashMap; 05.import android.content.Context; 06.import android.database.Cursor; 07.import android.data

25、base.sqlite.SQLiteDatabase; 08.import android.view.View; 09.import android.widget.AdapterView; 10.import android.widget.GridView; 11.import android.widget.LinearLayout; 12.import android.widget.SimpleAdapter; 13.import android.widget.AdapterView.OnItemClickListener; 14. 15.public class GVTable exten

26、ds LinearLayout 16. protected GridView gvTable,gvPage; 17. protected SimpleAdapter saPageID,saTable;/ 适配器 18. protected ArrayListHashMap srcPageID,srcTable;/ 数据源 19. 20. protected int TableRowCount=10;/分页时,每页的Row总数 21. protected int TableColCount=0;/每页col的数量 22. 23. protected SQLiteDatabase db; 24.

27、protected String rawSQL=; 25. protected Cursor curTable;/分页时使用的Cursor 26. protected OnTableClickListener clickListener;/整个分页控件被点击时的回调函数 27. protected OnPageSwitchListener switchListener;/分页切换时的回调函数 28. 29. public GVTable(Context context) 30. super(context); 31. this.setOrientation(VERTICAL);/垂直 32.

28、/- 33. gvTable=new GridView(context); 34. addView(gvTable, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 35. LayoutParams.WRAP_CONTENT);/宽长式样 36. 37. srcTable = new ArrayListHashMap(); 38. saTable = new SimpleAdapter(context, 39. srcTable,/ 数据来源 40. R.layout.items,/XML实现 41. new String Ite

29、mText ,/ 动态数组与ImageItem对应的子项 42. new int R.id.ItemText ); 43. / 添加并且显示 44. gvTable.setAdapter(saTable); 45. gvTable.setOnItemClickListener(new OnItemClickListener() 46. Override 47. public void onItemClick(AdapterView arg0, View arg1, int arg2, 48. long arg3) 49. int y=arg2/curTable.getColumnCount()

30、-1;/标题栏的不算 50. int x=arg2 % curTable.getColumnCount(); 51. if (clickListener != null/分页数据被点击 52. & y!=-1) /点中的不是标题栏时 53. clickListener.onTableClickListener(x,y,curTable); 54. 55. 56. ); 57. 58. /- 59. gvPage=new GridView(context); 60. gvPage.setColumnWidth(40);/设置每个分页按钮的宽度 61. gvPage.setNumColumns(G

31、ridView.AUTO_FIT);/分页按钮数量自动设置 62. addView(gvPage, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 63. LayoutParams.WRAP_CONTENT);/宽长式样 64. srcPageID = new ArrayListHashMap(); 65. saPageID = new SimpleAdapter(context, 66. srcPageID,/ 数据来源 67. R.layout.items,/XML实现 68. new String ItemText ,/ 动

32、态数组与ImageItem对应的子项 69. new int R.id.ItemText ); 70. 71. / 添加并且显示 72. gvPage.setAdapter(saPageID); 73. / 添加消息处理 74. gvPage.setOnItemClickListener(new OnItemClickListener() 75. Override 76. public void onItemClick(AdapterView arg0, View arg1, int arg2, 77. long arg3) 78. LoadTable(arg2);/根据所选分页读取对应的数据

33、 79. if(switchListener!=null)/分页切换时 80. switchListener.onPageSwitchListener(arg2,srcPageID.size(); 81. 82. 83. ); 84. 85. 86. /* 87. * 清除所有数据 88. */ 89. public void gvRemoveAll() 90. 91. if(this.curTable!=null) 92. curTable.close(); 93. srcTable.clear(); 94. saTable.notifyDataSetChanged(); 95. 96. s

34、rcPageID.clear(); 97. saPageID.notifyDataSetChanged(); 98. 99. 100. /* 101. * 读取指定ID的分页数据,返回当前页的总数据 102. * SQL:Select * From TABLE_NAME Limit 9 Offset 10; 103. * 表示从TABLE_NAME表获取数据,跳过10行,取9行 104. * param pageID 指定的分页ID 105. */ 106. protected void LoadTable(int pageID) 107. 108. if(curTable!=null)/释放

35、上次的数据 109. curTable.close(); 110. 111. String sql= rawSQL+ Limit +String.valueOf(TableRowCount)+ Offset +String.valueOf(pageID*TableRowCount); 112. curTable = db.rawQuery(sql, null); 113. 114. gvTable.setNumColumns(curTable.getColumnCount();/表现为表格的关键点! 115. TableColCount=curTable.getColumnCount(); 1

36、16. srcTable.clear(); 117. / 取得字段名称 118. int colCount = curTable.getColumnCount(); 119. for (int i = 0; i colCount; i+) 120. HashMap map = new HashMap(); 121. map.put(ItemText, curTable.getColumnName(i); 122. srcTable.add(map); 123. 124. 125. / 列举出所有数据 126. int recCount=curTable.getCount(); 127. for

37、 (int i = 0; i recCount; i+) /定位到一条数据 128. curTable.moveToPosition(i); 129. for(int ii=0;iicolCount;ii+)/定位到一条数据中的每个字段 130. 131. HashMap map = new HashMap(); 132. map.put(ItemText, curTable.getString(ii); 133. srcTable.add(map); 134. 135. 136. 137. saTable.notifyDataSetChanged(); 138. 139. 140. /* 1

38、41. * 设置表格的最多显示的行数 142. * param row 表格的行数 143. */ 144. public void gvSetTableRowCount(int row) 145. 146. TableRowCount=row; 147. 148. 149. /* 150. * 取得表格的最大行数 151. * return 行数 152. */ 153. public int gvGetTableRowCount() 154. 155. return TableRowCount; 156. 157. 158. /* 159. * 取得当前分页的Cursor 160. * r

39、eturn 当前分页的Cursor 161. */ 162. public Cursor gvGetCurrentTable() 163. 164. return curTable; 165. 166. 167. /* 168. * 准备分页显示数据 169. * param rawSQL sql语句 170. * param db 数据库 171. */ 172. public void gvReadyTable(String rawSQL,SQLiteDatabase db) 173. 174. this.rawSQL=rawSQL; 175. this.db=db; 176. 177.

40、178. /* 179. * 刷新分页栏,更新按钮数量 180. * param sql SQL语句 181. * param db 数据库 182. */ 183. public void gvUpdatePageBar(String sql,SQLiteDatabase db) 184. 185. Cursor rec = db.rawQuery(sql, null); 186. rec.moveToLast(); 187. long recSize=rec.getLong(0);/取得总数 188. rec.close(); 189. int pageNum=(int)(recSize/

41、TableRowCount) + 1;/取得分页数 190. 191. srcPageID.clear(); 192. for (int i = 0; i pageNum; i+) 193. HashMap map = new HashMap(); 194. map.put(ItemText, No. + String.valueOf(i);/ 添加图像资源的ID 195. 196. srcPageID.add(map); 197. 198. saPageID.notifyDataSetChanged(); 199. 200. 201. /- 202. /* 203. * 表格被点击时的回调函数 204. */

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!