Improve the SQLite paging table in Chapter 9 of Android

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

In the previous lecture on Android, SQLite reads data by PAGE and only displays data in text boxes. This time, we will go deeper and implement and encapsulate an SQL paging table control, data can be displayed on pages or in tables. Let's take a look at the animation program running in this article:

This SQL paging table control is mainly divided into two parts: "table area" and "page bar", which are implemented based on the gridview. The following describes how to use listview for table demos on Android. Compared with listview, the biggest advantage of listview is that the size of cells can be customized, and the length of a unit is short, but it is difficult to implement the structure of adaptive data tables; the biggest advantage of the gridview is the structure of the adaptive data table, but the cells are uniform in size... When the data table structure is changeable, we recommend that you use the gridview to implement the table.

The SQL paging Table Control Implemented in this article has the following features:

1. adaptive data table structure, with uniform size of cells;

2. pagination is supported;

3. The "table area" includes button Event Callback processing, and the "pagination bar" includes page switching Event Callback processing.

This article program code more, you can download the entire project source code: http://www.rayfile.com/files/72e78b68-f2e5-11df-8469-0015c55db73d/

The items. XML Code is as follows. It is the implementation of cells in the "table area" and "page bar:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout Android: Id = "@ + ID/linearlayout01" <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" Android: Background = "#555555" <br/> Android: layout_height = "wrap_content"> <br/> <textview Android: layout_below = "@ + ID/itemimage" Android: text = "textview01" <br/> Android: Id = "@ + ID/itemtext" Android: buffertype = "normal" <br/> Android: singleline = "true" Android: Background = "#000000" <br/> Android: layout_width = "fill_parent" Android: gravity = "center" <br/> Android: layout_margin = "1dip" Android: layout_gravity = "center" <br/> Android: layout_height = "wrap_content"> <br/> </textview> <br/> </linearlayout> <br/>

The main. XML Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" Android: Id = "@ + ID/mainlinearlayout"> <br/> <button Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" Android: Id = "@ + ID/btncreatedb" <br/> Android: TEXT = "create database"> </button> <br/> <button Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" Android: TEXT = "insert an experiment data string" Android: Id = "@ + ID/btninsertrec"> </button> <br/> <button Android: layout_height = "wrap_content" Android: id = "@ + ID/btnclose" <br/> Android: text = "Close Database" Android: layout_width = "fill_parent"> </button> <br/> </linearlayout> <br/>

Source code of the demo program testsqlite. Java:

Package COM. testsqlite; <br/> Import android. app. activity; <br/> Import android. database. cursor; <br/> Import android. database. sqlexception; <br/> Import android. database. SQLite. sqlitedatabase; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. linearlayout; <br/> Import android. widget. toast; <br/> public class testsqlite extends activity {<br/> gvtable; <br/> button btncreatedb, btninsert, btnclose; <br/> sqlitedatabase dB; <br/> int ID; // The ID accumulation mark when the record is added. It must be global. <br/> Private Static final string table_name = "Stu "; <br/> Private Static final string id = "ID"; <br/> Private Static final string name = "name "; <br/> Private Static final string phone = "phone"; <br/> Private Static final string address = "Address "; <br/> Private Static final string age = "Age"; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> btncreatedb = (button) This. findviewbyid (R. id. btncreatedb); <br/> btncreatedb. setonclicklistener (New clickevent (); <br/> btninsert = (button) This. findviewbyid (R. id. btninsertrec); <br/> btninsert. setonclicklistener (New clickevent (); <br/> btnclose = (button) This. findviewbyid (R. id. btnclose); <br/> btnclose. setonclicklistener (New clickevent (); <br/> table = new gvtable (this); <br/> table. gvsettablerowcount (8); // you can specify the total number of rows on each page. <br/> linearlayout Ly = (linearlayout) findviewbyid (R. id. mainlinearlayout); <br/> table. settableonclicklistener (New gvtable. ontableclicklistener () {<br/> @ override <br/> Public void ontableclicklistener (int x, int y, cursor c) {<br/> C. movetoposition (y); <br/> string STR = C. getstring (x) + "position: (" + String. valueof (x) + "," + String. valueof (y) + ")"; <br/> toast. maketext (testsqlite. this, STR, 1000 ). show (); <br/>}< br/>}); <br/> table. setonpageswitchlistener (New gvtable. onpageswitchlistener () {</P> <p> @ override <br/> Public void onpageswitchlistener (INT pageid, int pagecount) {<br/> string STR = "Total" + String. valueof (pagecount) + <br/> "current number" + String. valueof (pageid) + "page"; <br/> toast. maketext (testsqlite. this, STR, 1000 ). show (); <br/>}< br/>}); </P> <p> LY. addview (table); <br/>}< br/> class clickevent implements view. onclicklistener {<br/> @ override <br/> Public void onclick (view v) {<br/> If (V = btncreatedb) {<br/> createdb (); <br/>} else if (V = btninsert) {<br/> insertrecord (16); // insert 16 records <br/> table. gvupdatepagebar ("select count (*) from" + table_name, DB); <br/> table. gvreadytable ("select * from" + table_name, DB); <br/>} else if (V = btnclose) {<br/> table. gvremoveall (); <br/> dB. close (); </P> <p >}< br/>}</P> <p>/** <br/> * Create a database and a data table in the memory <br/> */<br/> void createdb () {<br/> // create a database in the memory <br/> DB = sqlitedatabase. create (null); <br/> log. E ("DB path", DB. getpath (); <br/> string amount = string. valueof (databaselist (). length); <br/> log. E ("DB amount", amount ); <br/> // create a data table <br/> string SQL = "CREATE TABLE" + table_name + "(" + <br/> ID + "text not null, "+ name +" text not null, "+ <br/> address +" text not null, "+ phone +" text not null, "+ <br/> age +" text not null "+ ");"; <br/> try {<br/> db.exe csql ("Drop table if exists" + table_name); <br/> db.exe csql (SQL ); <br/>} catch (sqlexception E) {}< br/>}< br/>/** <br/> * Insert n data records <br/> */<br/> void insertrecord (int n) {<br/> int Total = ID + N; <br/> for (; id <total; Id ++) {<br/> string SQL = "insert into" + table_name + "(" + <br/> ID + "," + name + "," + address + ", "+ phone +", "+ age <br/> +") values ('"+ String. valueof (ID) + "', 'Man', 'address', '000000', '18 ');"; <br/> try {<br/> db.exe csql (SQL); <br/>} catch (sqlexception E) {<br/>}</P> <p>}

Source code of gvtable. Java:

Package COM. testsqlite; <br/> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import android. content. context; <br/> Import android. database. cursor; <br/> Import android. database. SQLite. sqlitedatabase; <br/> Import android. view. view; <br/> Import android. widget. adapterview; <br/> Import android. widget. gridview; <br/> Import android. widget. linearlayout; <br/> Import android. widget. simpleadapt Er; <br/> Import android. widget. adapterview. onitemclicklistener; <br/> public class gvtable extends linearlayout {<br/> protected gridview gvtable, gvpage; <br/> protected simpleadapter sapageid, satable; // adapter <br/> protected arraylist <pashmap <string, string> srcpageid, srctable; // data source </P> <p> protected int tablerowcount = 10; // total number of rows on each page during pagination <br/> protected int tablecolcount = 0; // Number of Col on each page <br/> protected sqlit Edatabase dB; <br/> protected string rawsql = ""; <br/> protected cursor curtable; // cursor used for paging <br/> protected ontableclicklistener clicklistener; // callback function for the entire paging control when clicked <br/> protected onpageswitchlistener switchlistener; // callback function for page switching </P> <p> Public gvtable (context) {<br/> super (context); <br/> This. setorientation (vertical); // vertical <br/> // -------------------------------------------- <br/> gvtable = new gridview (Context); <br/> addview (gvtable, new linearlayout. layoutparams (layoutparams. fill_parent, <br/> layoutparams. wrap_content); // wide and long style </P> <p> srctable = new arraylist <pashmap <string, string> (); <br/> satable = new simpleadapter (context, <br/> srctable, // data source <br/> r. layout. items, // XML implementation <br/> New String [] {"itemtext"}, // subitem corresponding to the dynamic array and imageitem <br/> New int [] {R. id. itemtext}); <br/> // Add and display <br/> gvtable. s Etadapter (satable); <br/> gvtable. setonitemclicklistener (New onitemclicklistener () {<br/> @ override <br/> Public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, <br/> long arg3) {<br/> int y = arg2/curtable. getcolumncount ()-1; // The title bar does not count <br/> int x = arg2 % curtable. getcolumncount (); <br/> If (clicklistener! = NULL // The paging data is clicked. <br/> & Y! =-1) {// when the vertex is not the title bar <br/> clicklistener. ontableclicklistener (X, Y, curtable); <br/>}< br/> }); </P> <p> // ---------------------------------------- <br/> gvpage = new gridview (context); <br/> gvpage. setcolumnwidth (40); // set the width of each page button <br/> gvpage. setnumcolumns (gridview. auto_fit); // automatic setting of the number of paging buttons <br/> addview (gvpage, new linearlayout. layoutparams (layoutparams. fill_parent, <br/> layoutparams. wrap_content); // width and length <Br/> srcpageid = new arraylist <pashmap <string, string> (); <br/> sapageid = new simpleadapter (context, <br/> srcpageid, // data source <br/> r. layout. items, // XML implementation <br/> New String [] {"itemtext"}, // subitem corresponding to the dynamic array and imageitem <br/> New int [] {R. id. itemtext}); <br/> // Add and display <br/> gvpage. setadapter (sapageid); <br/> // Add Message Processing <br/> gvpage. setonitemclicklistener (New onitemclicklistener () {<br/> @ override <br/> Public voi D onitemclick (adapterview <?> Arg0, view arg1, int arg2, <br/> long arg3) {<br/> loadtable (arg2 ); // read the corresponding data based on the selected page <br/> If (switchlistener! = NULL) {// when switching pages <br/> switchlistener. onpageswitchlistener (arg2, srcpageid. size (); <br/>}< br/> }); <br/>}< br/>/** <br/> * clear all data <br/> */<br/> Public void gvremoveall () <br/>{< br/> If (this. curtable! = NULL) <br/> curtable. close (); <br/> srctable. clear (); <br/> satable. notifydatasetchanged (); </P> <p> srcpageid. clear (); <br/> sapageid. notifydatasetchanged (); </P> <p >}< br/>/** <br/> * reads the paging data of the specified ID, return the total data on the current page <br/> * SQL: Select * From table_name limit 9 offset 10; <br/> * Indicates retrieving data from the table_name table and skipping 10 rows, obtain the page ID specified by <br/> * @ Param pageid <br/> */<br/> protected void loadtable (INT pageid) <br/>{< br/> If (curtable! = NULL) // release the last data <br/> curtable. close (); </P> <p> string SQL = rawsql + "Limit" + String. valueof (tablerowcount) + "offset" + String. valueof (pageid * tablerowcount); <br/> curtable = dB. rawquery (SQL, null); </P> <p> gvtable. setnumcolumns (curtable. getcolumncount (); // displays the table's key points! <Br/> tablecolcount = curtable. getcolumncount (); <br/> srctable. clear (); <br/> // obtain the field name <br/> int colcount = curtable. getcolumncount (); <br/> for (INT I = 0; I <colcount; I ++) {<br/> hashmap <string, string> map = new hashmap <string, string> (); <br/> map. put ("itemtext", curtable. getcolumnname (I); <br/> srctable. add (MAP); <br/>}</P> <p> // list all data <br/> int reccount = curtable. getcount (); <br/> for (INT I = 0; I <reccount; I ++) {// locate a data entry <br/> curtable. movetoposition (I); <br/> for (INT II = 0; II <colcount; II ++) // locate each field in a data record <br/>{< br/> hashmap <string, string> map = new hashmap <string, string> (); <br/> map. put ("itemtext", curtable. getstring (ii); <br/> srctable. add (MAP); <br/>}</P> <p> satable. notifydatasetchanged (); <br/>}< br/>/** <br/> * set the maximum number of rows displayed in the table <br/> * @ Param row <br/> * /<br/> Public void gvsettablerowcount (INT row) <br/>{< br/> tablerowcount = row; <br/>}</P> <p>/** <br/> * obtain the maximum number of rows in the Table. <br/> * @ return rows <br/> */< br/> Public int gvgettablerowcount () <br/>{< br/> return tablerowcount; <br/>}</P> <p>/** <br/> * obtain the cursor of the current page <br/> * @ return the cursor of the current page <br/> */<br/> Public cursor gvgetcurrenttable () <br/>{< br/> return curtable; <br/>}</P> <p>/** <br/> * prepare to display data by page <br/> * @ Param rawsql SQL statement <br/> *@ param DB database <br/> */<br/> Public void gvreadytable (string rawsql, sqlitedatabase dB) <br/>{< br/> This. rawsql = rawsql; <br/> This. DB = dB; <br/>}</P> <p>/** <br/> * refresh the page bar, number of update buttons <br/> * @ Param SQL statement <br/> * @ Param DB database <br/> */<br/> Public void gvupdatepagebar (string SQL, sqlitedatabase dB) <br/>{< br/> cursor rec = dB. rawquery (SQL, null); <br/> rec. movetolast (); <br/> long recsize = rec. getlong (0); // get the total number <br/> rec. close (); <br/> int pagenum = (INT) (recsize/tablerowcount) + 1; // obtain the number of pages </P> <p> srcpageid. clear (); <br/> for (INT I = 0; I <pagenum; I ++) {<br/> hashmap <string, string> map = new hashmap <string, string> (); <br/> map. put ("itemtext", "no. "+ String. valueof (I); // ID of the image resource to be added <br/> srcpageid. add (MAP); <br/>}< br/> sapageid. notifydatasetchanged (); <br/>}< br/> // ------------------------------------------------------- <br/>/** <br/> * callback function when a table is clicked <br/> */<br/> Public void settableonclicklistener (ontableclicklistener click) {<br/> This. clicklistener = click; <br/>}</P> <p> Public interface ontableclicklistener {<br/> Public void ontableclicklistener (int x, int y, cursor C ); <br/>}< br/> // ------------------------------------------------------- <br/>/** <br/> * callback function when the page bar is clicked <br/> */<br /> Public void setonpageswitchlistener (onpageswitchlistener pageswitch) {<br/> This. switchlistener = pageswitch; <br/>}< br/> Public interface onpageswitchlistener {<br/> Public void onpageswitchlistener (INT pageid, int pagecount ); <br/>}< br/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.