惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 生如夏花之灿烂

本人讲课时录制的Android应用开发技术教学视频 jsoup使用样式class抓取数据时空格的处理 我的第一款anroid软件作品--短信精灵1.0 (原创)Android入门教程(三十六)------实现手机联系人的全选 Android入门教程(三十五)------在Android上使用ZXing识别条形码/二维码(转) - 生如夏花之灿烂 Android入门教程(三十四)------之多级树形菜单的实现 (转) Android入门教程(三十三)------之ListView自适应实现表格(转) Android入门教程(三十一)------SQLite分页读取(转) Android入门教程(三十)------之XML解析与生成(转) - 生如夏花之灿烂 Android入门教程(二十九)------之BroadcastReceiver (转) Android入门教程(二十八)------之Service(转) Android入门教程(二十七)------之Style与Theme (转) Android入门教程(二十六)------之ActivityGroup + GridView 实现Tab分页标签(转) Android入门教程(二十五)------之画图(转) Android入门教程(二十四)------之Gallery + ImageSwitcher(转) Android入门教程(二十三)------之Gallery(转) Android入门教程(二十二)------之TabHost,TabWidget(转) Android入门教程(二十一)------之PopupWindow (转) Android入门教程(二十)之--之AlertDialog(转)
Android入门教程(三十二)------之SQLite分页表格(转)
生如夏花之灿烂 · 2011-08-11 · via 博客园 - 生如夏花之灿烂

 上次讲的Android上的SQLite分页读取,只用文本框显示数据而已,这次就讲得更加深入些,实现并封装一个SQL分页表格控件,不仅支持分页还是以表格的形式展示数据。先来看看本文程序运行的动画:

       这个SQL分页表格控件主要分为“表格区”和“分页栏”这两部分,这两部分都是基于GridView实现的。网上介绍Android上实现表格的DEMO一般都用ListView。ListView与GridView对比,ListView最大的优势是格单元的大小可以自定义,可以某单元长某单元短,但是难于实现自适应数据表的结构;而GridView最大的优势就是自适应数据表的结构,但是格单元统一大小。。。对于数据表结构多变的情况,建议使用GridView实现表格。

本文实现的SQL分页表格控件有以下特点:

1.自适应数据表结构,但是格单元统一大小;

2.支持分页;

3.“表格区”有按键事件回调处理,“分页栏”有分页切换事件回调处理。

本文程序代码较多,可以到这里下载整个工程的源码:http://www.rayfile.com/files/72e78b68-f2e5-11df-8469-0015c55db73d/

items.xml的代码如下,它是“表格区”和“分页栏”的格单元实现:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout android:id="@+id/LinearLayout01"  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent" android:background="#555555"  
  5.     android:layout_height="wrap_content">  
  6.     <TextView android:layout_below="@+id/ItemImage" android:text="TextView01"  
  7.         android:id="@+id/ItemText" android:bufferType="normal"  
  8.         android:singleLine="true" android:background="#000000"  
  9.         android:layout_width="fill_parent" android:gravity="center"  
  10.         android:layout_margin="1dip" android:layout_gravity="center"  
  11.         android:layout_height="wrap_content">  
  12.     </TextView>  
  13. </LinearLayout>  

main.xml的代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" android:id="@+id/MainLinearLayout">  
  5.     <Button android:layout_height="wrap_content"  
  6.         android:layout_width="fill_parent" android:id="@+id/btnCreateDB"  
  7.         android:text="创建数据库"></Button>  
  8.     <Button android:layout_height="wrap_content"  
  9.         android:layout_width="fill_parent" android:text="插入一串实验数据" android:id="@+id/btnInsertRec"></Button>  
  10.     <Button android:layout_height="wrap_content" android:id="@+id/btnClose"  
  11.         android:text="关闭数据库" android:layout_width="fill_parent"></Button>  
  12. </LinearLayout>  

演示程序testSQLite.java的源码:

  1. package com.testSQLite;  
  2. import android.app.Activity;  
  3. import android.database.Cursor;  
  4. import android.database.SQLException;  
  5. import android.database.sqlite.SQLiteDatabase;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.LinearLayout;  
  11. import android.widget.Toast;  
  12. public class testSQLite extends Activity {  
  13.     GVTable table;  
  14.     Button btnCreateDB, btnInsert, btnClose;  
  15.     SQLiteDatabase db;  
  16.     int id;  
  17.     private static final String TABLE_NAME = "stu";  
  18.     private static final String ID = "id";  
  19.     private static final String NAME = "name";  
  20.     private static final String PHONE = "phone";  
  21.     private static final String ADDRESS = "address";  
  22.     private static final String AGE = "age";  
  23.       
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.         btnCreateDB = (Button) this.findViewById(R.id.btnCreateDB);  
  29.         btnCreateDB.setOnClickListener(new ClickEvent());  
  30.         btnInsert = (Button) this.findViewById(R.id.btnInsertRec);  
  31.         btnInsert.setOnClickListener(new ClickEvent());  
  32.         btnClose = (Button) this.findViewById(R.id.btnClose);  
  33.         btnClose.setOnClickListener(new ClickEvent());  
  34.         table=new GVTable(this);  
  35.         table.gvSetTableRowCount(8);  
  36.         LinearLayout ly = (LinearLayout) findViewById(R.id.MainLinearLayout);  
  37.         table.setTableOnClickListener(new GVTable.OnTableClickListener() {  
  38.             @Override  
  39.             public void onTableClickListener(int x,int y,Cursor c) {  
  40.                 c.moveToPosition(y);  
  41.                 String str=c.getString(x)+" 位置:("+String.valueOf(x)+","+String.valueOf(y)+")";  
  42.                 Toast.makeText(testSQLite.this, str, 1000).show();  
  43.             }  
  44.         });  
  45.         table.setOnPageSwitchListener(new GVTable.OnPageSwitchListener() {  
  46.               
  47.             @Override  
  48.             public void onPageSwitchListener(int pageID,int pageCount) {  
  49.                 String str="共有"+String.valueOf(pageCount)+  
  50.                 " 当前第"+String.valueOf(pageID)+"页";  
  51.                 Toast.makeText(testSQLite.this, str, 1000).show();  
  52.             }  
  53.         });  
  54.           
  55.         ly.addView(table);  
  56.     }  
  57.     class ClickEvent implements View.OnClickListener {  
  58.         @Override  
  59.         public void onClick(View v) {  
  60.             if (v == btnCreateDB) {  
  61.                 CreateDB();  
  62.             } else if (v == btnInsert) {  
  63.                 InsertRecord(16);  
  64.                 table.gvUpdatePageBar("select count(*) from " + TABLE_NAME,db);  
  65.                 table.gvReadyTable("select * from " + TABLE_NAME,db);  
  66.             }else if (v == btnClose) {  
  67.                 table.gvRemoveAll();  
  68.                 db.close();  
  69.                   
  70.             }  
  71.         }  
  72.     }  
  73.       
  74.      
  75.  
  76.   
  77.     void CreateDB() {  
  78.           
  79.         db = SQLiteDatabase.create(null);  
  80.         Log.e("DB Path", db.getPath());  
  81.         String amount = String.valueOf(databaseList().length);  
  82.         Log.e("DB amount", amount);  
  83.           
  84.         String sql = "CREATE TABLE " + TABLE_NAME + " (" +   
  85.                 ID  + " text not null, " + NAME + " text not null," +  
  86.                 ADDRESS + " text not null, " + PHONE + " text not null," +  
  87.                 AGE + " text not null "+");";  
  88.         try {  
  89.             db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);  
  90.             db.execSQL(sql);  
  91.         } catch (SQLException e) {}  
  92.     }  
  93.      
  94.  
  95.   
  96.     void InsertRecord(int n) {  
  97.         int total = id + n;  
  98.         for (; id < total; id++) {  
  99.             String sql = "insert into " + TABLE_NAME + " (" +   
  100.             ID + ", " + NAME+", " + ADDRESS+", " + PHONE+", "+AGE  
  101.                     + ") values('" + String.valueOf(id) + "', 'man','address','123456789','18');";  
  102.             try {  
  103.                 db.execSQL(sql);  
  104.             } catch (SQLException e) {  
  105.             }  
  106.         }  
  107.     }  
  108.       
  109.       
  110. }  

分页表格控件GVTable.java的源码:

  1. package com.testSQLite;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import android.content.Context;  
  5. import android.database.Cursor;  
  6. import android.database.sqlite.SQLiteDatabase;  
  7. import android.view.View;  
  8. import android.widget.AdapterView;  
  9. import android.widget.GridView;  
  10. import android.widget.LinearLayout;  
  11. import android.widget.SimpleAdapter;  
  12. import android.widget.AdapterView.OnItemClickListener;  
  13. public class GVTable extends LinearLayout {  
  14.     protected GridView gvTable,gvPage;    
  15.     protected SimpleAdapter saPageID,saTable;  
  16.     protected ArrayList<HashMap<String, String>> srcPageID,srcTable;  
  17.       
  18.     protected int TableRowCount=10;  
  19.     protected int TableColCount=0;  
  20.     protected SQLiteDatabase db;  
  21.     protected String rawSQL="";  
  22.     protected Cursor curTable;  
  23.     protected OnTableClickListener clickListener;  
  24.     protected OnPageSwitchListener switchListener;  
  25.       
  26.     public GVTable(Context context) {  
  27.         super(context);  
  28.         this.setOrientation(VERTICAL);  
  29.           
  30.         gvTable=new GridView(context);  
  31.         addView(gvTable, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,  
  32.                 LayoutParams.WRAP_CONTENT));  
  33.           
  34.         srcTable = new ArrayList<HashMap<String, String>>();  
  35.         saTable = new SimpleAdapter(context,  
  36.                 srcTable,  
  37.                 R.layout.items,  
  38.                 new String[] { "ItemText" },  
  39.                 new int[] { R.id.ItemText });  
  40.           
  41.         gvTable.setAdapter(saTable);  
  42.         gvTable.setOnItemClickListener(new OnItemClickListener(){  
  43.             @Override  
  44.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  45.                     long arg3) {  
  46.                 int y=arg2/curTable.getColumnCount()-1;  
  47.                 int x=arg2 % curTable.getColumnCount();  
  48.                 if (clickListener != null  
  49.                         && y!=-1) {  
  50.                     clickListener.onTableClickListener(x,y,curTable);  
  51.                 }  
  52.             }  
  53.         });  
  54.           
  55.           
  56.         gvPage=new GridView(context);  
  57.         gvPage.setColumnWidth(40);  
  58.         gvPage.setNumColumns(GridView.AUTO_FIT);  
  59.         addView(gvPage, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,  
  60.                 LayoutParams.WRAP_CONTENT));  
  61.         srcPageID = new ArrayList<HashMap<String, String>>();  
  62.         saPageID = new SimpleAdapter(context,  
  63.                 srcPageID,  
  64.                 R.layout.items,  
  65.                 new String[] { "ItemText" },  
  66.                 new int[] { R.id.ItemText });  
  67.           
  68.         gvPage.setAdapter(saPageID);  
  69.           
  70.         gvPage.setOnItemClickListener(new OnItemClickListener(){  
  71.             @Override  
  72.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  73.                     long arg3) {  
  74.                 LoadTable(arg2);  
  75.                 if(switchListener!=null){  
  76.                     switchListener.onPageSwitchListener(arg2,srcPageID.size());  
  77.                 }  
  78.             }  
  79.         });  
  80.     }  
  81.      
  82.  
  83.   
  84.     public void gvRemoveAll()  
  85.     {  
  86.         if(this.curTable!=null)  
  87.             curTable.close();  
  88.         srcTable.clear();  
  89.         saTable.notifyDataSetChanged();  
  90.       
  91.         srcPageID.clear();  
  92.         saPageID.notifyDataSetChanged();  
  93.           
  94.     }  
  95.      
  96.  
  97.  
  98.  
  99.  
  100.   
  101.     protected void LoadTable(int pageID)  
  102.     {  
  103.         if(curTable!=null)  
  104.             curTable.close();  
  105.           
  106.         String sql= rawSQL+" Limit "+String.valueOf(TableRowCount)+ " Offset " +String.valueOf(pageID*TableRowCount);  
  107.         curTable = db.rawQuery(sql, null);  
  108.           
  109.         gvTable.setNumColumns(curTable.getColumnCount());  
  110.         TableColCount=curTable.getColumnCount();  
  111.         srcTable.clear();  
  112.           
  113.         int colCount = curTable.getColumnCount();  
  114.         for (int i = 0; i < colCount; i++) {  
  115.             HashMap<String, String> map = new HashMap<String, String>();  
  116.             map.put("ItemText", curTable.getColumnName(i));  
  117.             srcTable.add(map);  
  118.         }  
  119.           
  120.           
  121.         int recCount=curTable.getCount();  
  122.         for (int i = 0; i < recCount; i++) {  
  123.             curTable.moveToPosition(i);  
  124.             for(int ii=0;ii<colCount;ii++)  
  125.             {  
  126.                 HashMap<String, String> map = new HashMap<String, String>();  
  127.                 map.put("ItemText", curTable.getString(ii));  
  128.                 srcTable.add(map);  
  129.             }  
  130.         }  
  131.           
  132.         saTable.notifyDataSetChanged();  
  133.     }  
  134.      
  135.  
  136.  
  137.   
  138.     public void gvSetTableRowCount(int row)  
  139.     {  
  140.         TableRowCount=row;  
  141.     }  
  142.       
  143.      
  144.  
  145.  
  146.   
  147.     public int gvGetTableRowCount()  
  148.     {  
  149.         return TableRowCount;  
  150.     }  
  151.       
  152.      
  153.  
  154.  
  155.   
  156.     public Cursor gvGetCurrentTable()  
  157.     {  
  158.         return curTable;  
  159.     }  
  160.           
  161.      
  162.  
  163.  
  164.  
  165.   
  166.     public void gvReadyTable(String rawSQL,SQLiteDatabase db)  
  167.     {  
  168.         this.rawSQL=rawSQL;  
  169.         this.db=db;  
  170.     }  
  171.       
  172.      
  173.  
  174.  
  175.  
  176.   
  177.     public void gvUpdatePageBar(String sql,SQLiteDatabase db)  
  178.     {  
  179.         Cursor rec = db.rawQuery(sql, null);  
  180.         rec.moveToLast();  
  181.         long recSize=rec.getLong(0);  
  182.         rec.close();  
  183.         int pageNum=(int)(recSize/TableRowCount) + 1;  
  184.           
  185.         srcPageID.clear();  
  186.         for (int i = 0; i < pageNum; i++) {  
  187.             HashMap<String, String> map = new HashMap<String, String>();  
  188.             map.put("ItemText""No." + String.valueOf(i));  
  189.             srcPageID.add(map);  
  190.         }  
  191.         saPageID.notifyDataSetChanged();  
  192.     }  
  193.       
  194.      
  195.  
  196.   
  197.     public void setTableOnClickListener(OnTableClickListener click) {  
  198.         this.clickListener = click;  
  199.     }  
  200.       
  201.     public interface OnTableClickListener {  
  202.         public void onTableClickListener(int x,int y,Cursor c);  
  203.     }  
  204.       
  205.      
  206.  
  207.   
  208.     public void setOnPageSwitchListener(OnPageSwitchListener pageSwitch) {  
  209.         this.switchListener = pageSwitch;  
  210.     }  
  211.     public interface OnPageSwitchListener {  
  212.         public void onPageSwitchListener(int pageID,int pageCount);  
  213.     }  
  214. }