转自http://www.android-study.com/jichuzhishi/496.html
实现并封装一个SQL分页表格控件,不仅支持分页还是以表格的形式展示数据。先来看看本文程序运行的动画:
这个SQL分页表格控件主要分为“表格区”和“分页栏”这两部分,这两部分都是基于GridView实现的。网上介绍Android上实现表格的DEMO一般都用ListView。ListView与GridView对比,ListView最大的优势是格单元的大小可以自定义,可以某单元长某单元短,但是难于实现自适应数据表的结构;而GridView最大的优势就是自适应数据表的结构,但是格单元统一大小。。。对于数据表结构多变的情况,建议使用GridView实现表格。
本文实现的SQL分页表格控件有以下特点:
1、自适应数据表结构,但是格单元统一大小。
2、支持分页。
3、“表格区”有按键事件回调处理,“分页栏”有分页切换事件回调处理。
本文程序代码较多,可以到这里下载整个工程的源码:testSQLite.rar
items.xml的代码如下,它是“表格区”和“分页栏”的格单元实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:id = "@+id/LinearLayout01"
android:layout_width = "fill_parent"
android:background = "#555555"
android:layout_height = "wrap_content" >
< TextView
android:id = "@+id/ItemText"
android:layout_below = "@+id/ItemImage"
android:text = "TextView01"
android:bufferType = "normal"
android:singleLine = "true"
android:background = "#000000"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:gravity = "center"
android:layout_margin = "1dip"
android:layout_gravity = "center"
/>
</ LinearLayout >
|
main.xml的代码如下:
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
|
<? 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"
android:id = "@+id/MainLinearLayout" >
< Button
android:id = "@+id/btnCreateDB"
android:layout_height = "wrap_content"
android:layout_width = "fill_parent"
android:text = "创建数据库"
/>
< Button
android:id = "@+id/btnInsertRec"
android:layout_height = "wrap_content"
android:layout_width = "fill_parent"
android:text = "插入一串实验数据"
/>
< Button
android:id = "@+id/btnClose"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
/>
</ LinearLayout >
|
演示程序testsqlite.java的源码:
@H_404_814@
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
|
package com.testsqlite;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlException;
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,btnInsert,btnClose;
sqliteDatabase db;
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 = "age" ;
@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());
table = new GVTable( this );
table.gvSetTableRowCount( 8 ); // 设置每个分页的ROW总数
LinearLayout ly = (LinearLayout) findViewById(R.id.MainLinearLayout);
table.setTableOnClickListener( new GVTable.OnTableClickListener() {
@Override
public 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.OnPageSwitchListener() {
@Override
public void onPageSwitchListener( int pageID, int pageCount) {
String str = "共有" + String.valueOf(pageCount) + " 当前第"
+ String.valueOf(pageID) + "页" ;
Toast.makeText(testsqlite. this , 1000 ).show();
}
});
ly.addView(table);
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
if (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 = 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," + ADDRESS
+ " text not null," + PHONE + " text not null," + AGE
+ " text not null " + ");" ;
try {
db.execsql( "DROP TABLE IF EXISTS " + TABLE_NAME);
} 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','address','123456789','18');" ;
try {
} catch (sqlException e) {
}
}
}
}
|