- 浏览: 5782979 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (890)
- WindowsPhone (0)
- android (88)
- android快速迭代 (17)
- android基础 (34)
- android进阶 (172)
- android高级 (0)
- android拾遗 (85)
- android动画&效果 (68)
- Material Design (13)
- LUA (5)
- j2me (32)
- jQuery (39)
- spring (26)
- hibernate (20)
- struts (26)
- tomcat (9)
- javascript+css+html (62)
- jsp+servlet+javabean (14)
- java (37)
- velocity+FCKeditor (13)
- linux+批处理 (9)
- mysql (19)
- MyEclipse (9)
- ajax (7)
- wap (8)
- j2ee+apache (24)
- 其他 (13)
- phonegap (35)
最新评论
-
Memories_NC:
本地lua脚本终于执行成功了,虽然不是通过redis
java中调用lua脚本语言1 -
ZHOU452840622:
大神://处理返回的接收状态 这个好像没有监听到 遇 ...
android 发送短信的两种方式 -
PXY:
拦截部分地址,怎么写的for(int i=0;i<lis ...
判断是否登录的拦截器SessionFilter -
maotou1988:
Android控件之带清空按钮(功能)的AutoComplet ...
自定义AutoCompleteTextView -
yangmaolinpl:
希望有表例子更好。。。,不过也看明白了。
浅谈onInterceptTouchEvent、onTouchEvent与onTouch
http://blog.csdn.net/hellogv/archive/2010/11/16/6011934.aspx
Android包含了常用于嵌入式系统的SQLite,免去了开发者自己移植安装的功夫。SQLite 支持多数 SQL92 标准,很多常用的SQL命令都能在SQLite上面使用,除此之外Android还提供了一系列自定义的方法去简化对SQLite数据库的操作。不过有跨平台需求的程序就建议使用标准的SQL语句,毕竟这样容易在多个平台之间移植。
先贴出本文程序运行的结果:
本文主要讲解了SQLite的基本用法,如:创建数据库,使用SQL命令查询数据表、插入数据,关闭数据库,以及使用GridView实现了一个分页栏(关于GridView的用法),用于把数据分页显示。
分页栏的pagebuttons.xml的源码如下:
main.xml的源码如下:
本文程序源码如下:
Android包含了常用于嵌入式系统的SQLite,免去了开发者自己移植安装的功夫。SQLite 支持多数 SQL92 标准,很多常用的SQL命令都能在SQLite上面使用,除此之外Android还提供了一系列自定义的方法去简化对SQLite数据库的操作。不过有跨平台需求的程序就建议使用标准的SQL语句,毕竟这样容易在多个平台之间移植。
先贴出本文程序运行的结果:
本文主要讲解了SQLite的基本用法,如:创建数据库,使用SQL命令查询数据表、插入数据,关闭数据库,以及使用GridView实现了一个分页栏(关于GridView的用法),用于把数据分页显示。
分页栏的pagebuttons.xml的源码如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="4dip" android:layout_width="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_below="@+id/ItemImage" android:layout_height="wrap_content" android:text="TextView01" android:layout_centerHorizontal="true" android:id="@+id/ItemText"> </TextView> </RelativeLayout>
main.xml的源码如下:
<?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"> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnCreateDB" android:text="创建数据库"></Button> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="插入一串实验数据" android:id="@+id/btnInsertRec"></Button> <Button android:layout_height="wrap_content" android:id="@+id/btnClose" android:text="关闭数据库" android:layout_width="fill_parent"></Button> <EditText android:text="@+id/EditText01" android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="256dip"></EditText> <GridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="32dip" android:numColumns="auto_fit" android:columnWidth="40dip"></GridView> </LinearLayout>
本文程序源码如下:
package com.testSQLite; import java.util.ArrayList; import java.util.HashMap; 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.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.GridView; import android.widget.SimpleAdapter; public class testSQLite extends Activity { /** Called when the activity is first created. */ Button btnCreateDB, btnInsert, btnClose; EditText edtSQL;//显示分页数据 SQLiteDatabase db; int id;//添加记录时的id累加标记,必须全局 static final int PageSize=10;//分页时,每页的数据总数 private static final String TABLE_NAME = "stu"; private static final String ID = "id"; private static final String NAME = "name"; SimpleAdapter saPageID;// 分页栏适配器 ArrayList<HashMap<String, String>> lstPageID;// 分页栏的数据源,与PageSize和数据总数相关 @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()); edtSQL=(EditText)this.findViewById(R.id.EditText01); GridView gridview = (GridView) findViewById(R.id.gridview);//分页栏控件 // 生成动态数组,并且转入数据 lstPageID = new ArrayList<HashMap<String, String>>(); // 生成适配器的ImageItem <====> 动态数组的元素,两者一一对应 saPageID = new SimpleAdapter(testSQLite.this, // 没什么解释 lstPageID,// 数据来源 R.layout.pagebuttons,//XML实现 new String[] { "ItemText" }, new int[] { R.id.ItemText }); // 添加并且显示 gridview.setAdapter(saPageID); // 添加消息处理 gridview.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { LoadPage(arg2);//根据所选分页读取对应的数据 } }); } class ClickEvent implements View.OnClickListener { @Override public void onClick(View v) { if (v == btnCreateDB) { CreateDB(); } else if (v == btnInsert) { InsertRecord(16);//插入16条记录 RefreshPage(); }else if (v == btnClose) { db.close(); } } } /* * 读取指定ID的分页数据 * SQL:Select * From TABLE_NAME Limit 9 Offset 10; * 表示从TABLE_NAME表获取数据,跳过10行,取9行 */ void LoadPage(int pageID) { String sql= "select * from " + TABLE_NAME + " Limit "+String.valueOf(PageSize)+ " Offset " +String.valueOf(pageID*PageSize); Cursor rec = db.rawQuery(sql, null); setTitle("当前分页的数据总数:"+String.valueOf(rec.getCount())); // 取得字段名称 String title = ""; int colCount = rec.getColumnCount(); for (int i = 0; i < colCount; i++) title = title + rec.getColumnName(i) + " "; // 列举出所有数据 String content=""; int recCount=rec.getCount(); for (int i = 0; i < recCount; i++) {//定位到一条数据 rec.moveToPosition(i); for(int ii=0;ii<colCount;ii++)//定位到一条数据中的每个字段 { content=content+rec.getString(ii)+" "; } content=content+"\r\n"; } edtSQL.setText(title+"\r\n"+content);//显示出来 rec.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 " + ");"; try { 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 + ") values('" + String.valueOf(id) + "', 'test');"; try { db.execSQL(sql); } catch (SQLException e) { } } } /* * 插入之后刷新分页 */ void RefreshPage() { String sql = "select count(*) from " + TABLE_NAME; Cursor rec = db.rawQuery(sql, null); rec.moveToLast(); long recSize=rec.getLong(0);//取得总数 rec.close(); int pageNum=(int)(recSize/PageSize) + 1;//取得分页数 lstPageID.clear(); for (int i = 0; i < pageNum; i++) { HashMap<String, String> map = new HashMap<String, String>(); map.put("ItemText", "No." + String.valueOf(i)); lstPageID.add(map); } saPageID.notifyDataSetChanged(); } }
发表评论
-
NestedScrollView滚动到顶部固定子View悬停挂靠粘在顶端
2018-10-31 20:45 6821网上有一个StickyScrollView,称之为粘性Scro ... -
自定义Behavior实现AppBarLayout越界弹性效果
2017-03-31 09:33 10258一、继承AppBarLayout.Beha ... -
Android - 一种相似图片搜索算法的实现
2017-03-31 09:33 2576算法 缩小尺寸。 将图片缩小到8x8的尺寸,总共64个 ... -
使用SpringAnimation实现带下拉弹簧动画的 ScrollView
2017-03-30 11:30 2802在刚推出的 Support Library 25.3.0 里面 ... -
Android为应用添加角标(Badge)
2017-03-30 11:21 61161.需求简介 角标是什么意思呢? 看下图即可明了: 可 ... -
Android端与笔记本利用局域网进行FTP通信
2017-03-23 10:17 935先看图 打开前: 打开后: Activity类 ... -
PorterDuffColorFilter 在项目中的基本使用
2017-03-03 10:58 1307有时候标题栏会浮在内容之上,而内容会有颜色的变化,这时候就要求 ... -
ColorAnimationView 实现了滑动Viewpager 时背景色动态变化的过渡效果
2017-02-24 09:41 2181用法在注释中: import android.anima ... -
迷你轻量级全方向完美滑动处理侧滑控件SlideLayout
2017-01-16 16:53 2555纯手工超级迷你轻量级全方向完美滑动处理侧滑控件(比官方 sup ... -
Effect
2017-01-05 09:57 0https://github.com/JetradarMobi ... -
动态主题库Colorful,容易地改变App的配色方案
2016-12-27 14:49 2532Colorful是一个动态主题库,允许您很容易地改变App的配 ... -
对视图的对角线切割DiagonalView
2016-12-27 14:23 1079提供对视图的对角线切割,具有很好的用户定制 基本用法 ... -
仿淘宝京东拖拽商品详情页上下滚动黏滞效果
2016-12-26 16:53 3429比较常用的效果,有现成的,如此甚好!:) import ... -
让任意view具有滑动效果的SlideUp
2016-12-26 09:26 1671基本的类,只有一个: import android.a ... -
AdvancedWebView
2016-12-21 09:44 16https://github.com/delight-im/A ... -
可设置圆角背景边框的按钮, 通过调节色彩明度自动计算按下(pressed)状态颜色
2016-11-02 22:13 1871可设置圆角背景边框的的按钮, 通过调节色彩明度自动计算按下(p ... -
网络请求库相关
2016-10-09 09:35 62https://github.com/amitshekhari ... -
ASimpleCache一个简单的缓存框架
2015-10-26 22:53 2130ASimpleCache 是一个为android制定的 轻量级 ... -
使用ViewDragHelper实现的DragLayout开门效果
2015-10-23 10:55 3351先看一下图,有个直观的了解,向下拖动handle就“开门了”: ... -
保证图片长宽比的同时拉伸图片ImageView
2015-10-16 15:40 3684按比例放大图片,不拉伸失真 import android. ...
相关推荐
Android提高第八篇之SQLite分页读取
Android提高第九篇之SQLite分页表格
上次讲的Android上的SQLite分页读取,只用文本框显示数据而已,这次就讲得更加深入些,实现并封装一个SQL分页表格控件,不仅支持分页还是以表格的形式展示数据。
android的分页加载是非常有用且非常常见的功能,我对分页加载做了一个例子很实用,希望能给你带来帮助。
前言:最近做基于openfire聊天(仿QQ、微信)翻页查看聊天记录,为此做了根据时间倒序查看聊天记录,先声明这demo是根据id来倒序(原理和时间倒序一样) 文章地址:...
21天学习android开发教程之SQLite分页读取,Android包含了常用于嵌入式系统的SQLite,免去了开发者自己移植安装的功夫,感兴趣的朋友可以参考一下
主要介绍了Android的SQLite分页读取实现方法,在Android项目开发中非常实用,需要的朋友可以参考下
适合初学者 知识点: 1、listView分页加载 2、利用SparseArray实现ViewHolder的缓存 3、java反射机制读取sqlite查询结果 4、sqlite数据库分页读取 5、文件拷贝操作
SQLite 查询所有 分页查询 查询个数
android sqlite操作源码,包括增删改查,环境要求4.0以上
android sqlite数据库的分页显示
SQLite分页加载
自己写的PHP sqlite 分页代码程序
android之SQLite数据库开发详解: Android 开发中使用 SQLite 数据库 简介: SQLite 是一款非常流行的嵌入式数据库,它支持 SQL 查询,并且只用很少的内存。Android 在运行时集成了 SQLite,所以每个 Android 应用...
Android 数据库SQLite 详解,简单学好SQLite
从Sqlite数据库中查询出数据,进行分页显示,计算总页数,可以点击上一页,下一页。也可以自定义,每页要显示的数量,适合初学者学习用,高手指点下需要改进的地方。
这是一个android listview分页查询显示的demo,使用sqlite数据库,然后每次显示10条,上拉加载更多数据,studio项目
一个简单的Android SQLite ORM框架不想花资源分的同学可以上我的github主页下载:https://github.com/chenyihan/Simple-SQLite-ORM-Android,因为要传到github,所以代码中的注释和doc文档都是英文的,对自己英文不...