Improve the SQLite paging table in Chapter 9 of Android

Source: Internet
Author: User

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:

View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout android: id = "@ + id/LinearLayout01"
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" android: background = "#555555"
Android: layout_height = "wrap_content">
<TextView android: layout_below = "@ + id/ItemImage" android: text = "TextView01"
Android: id = "@ + id/ItemText" android: bufferType = "normal"
Android: singleLine = "true" android: background = "#000000"
Android: layout_width = "fill_parent" android: gravity = "center"
Android: layout_margin = "1dip" android: layout_gravity = "center"
Android: layout_height = "wrap_content">
</TextView>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout android: id = "@ + id/LinearLayout01"
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" android: background = "#555555"
Android: layout_height = "wrap_content">
<TextView android: layout_below = "@ + id/ItemImage" android: text = "TextView01"
Android: id = "@ + id/ItemText" android: bufferType = "normal"
Android: singleLine = "true" android: background = "#000000"
Android: layout_width = "fill_parent" android: gravity = "center"
Android: layout_margin = "1dip" android: layout_gravity = "center"
Android: layout_height = "wrap_content">
</TextView>
</LinearLayout>
 

The main. xml Code is as follows:

View plaincopy to clipboardprint?
<? 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: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/btnCreateDB"
Android: text = "creating a database"> </Button>
<Button android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "insert a series of experiment data"Android: id =" @ + id/btnInsertRec "> </Button>
<Button android: layout_height = "wrap_content" android: id = "@ + id/btnClose"
Android: text = "" android: layout_width = "fill_parent"> </Button>
</LinearLayout>
<? 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: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/btnCreateDB"
Android: text = "creating a database"> </Button>
<Button android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "insert a series of experiment data"Android: id =" @ + id/btnInsertRec "> </Button>
<Button android: layout_height = "wrap_content" android: id = "@ + id/btnClose"
Android: text = "" android: layout_width = "fill_parent"> </Button>
</LinearLayout>
 

Source code of the demo program testSQLite. java:

View plaincopy to clipboardprint?
Package com. testSQLite;
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. Button;
Import android. widget. LinearLayout;
Import android. widget. Toast;
Public class testSQLite extends Activity {
GVTable;
Button btnCreateDB, btnInsert, btnClose;
SQLiteDatabase db;
Int id; // The id accumulation mark when the record is added. It must be global.
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. findView

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.