SimpleCursorAdapter和ListView的结合使用

我们在用SQLite查数据的时候,经常会用到Cursor这个游标,我们希望能将游标指向的数据直接绑定到ListView中,这样就免去了将游标数据取出然后转换到SimpleAdapter中的麻烦。今天我们来演示下这个适配器如何使用。
思路:通过传统的方法执行查询操作,返回一个Cursor,将这个游标放入到SimpleCursorAapter的构造函数中即可,最后setAdapter
MainActivity.java
package com.kale.cursoradapter; import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter; public class MainActivity extends Activity { DatabaseManager dbManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到一个数据层操作对象
dbManager = new DatabaseManager(this);
//因为在数据库建立的时候我们已经建立一个表了,所以这里就可以直接插入数据了
String insertSql = "insert into test_table (name,age) values('kale',20)";
//用循环的方式来插入20条数据
for (int i = 0; i < 20; i++) {
dbManager.executeSql(insertSql);
}
// 这个游标查询到的数据中必须有一个列名为_id否则会报错,所以写sql语句的时候必须要查到_id。显不显示这个id到无所谓
String sql = "select _id,name,age from test_table";
//得到一个Cursor,这个将要放入适配器中
Cursor cursor = dbManager.executeSql(sql, null);
// 最后一个参数flags是一个标识,标识当数据改变调用onContentChanged()的时候,是
// 否通知ContentProvider数据的改变,如果无需监听ContentProvider的改变,则可以传0。
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.item, cursor, new String[] { "name", "age" },
new int[] { R.id.name, R.id.age }, 0); ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
} }
注意:这里查询到的数据中必须有一列是_id,否则会报错。所以在建表的时候就应该建立这一列。

后面两个类基本就是直接从我之前的文章中copy过来的,方便以后使用。
数据库对象类——DatabaseManager
package com.kale.cursoradapter; import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException; public class DatabaseManager{ DatabaseHelper mDbHelper = null;
//操作数据库的实例
static SQLiteDatabase mDb = null;
Context mContext = null; public DatabaseManager(Context context) {
mContext = context;
mDbHelper = DatabaseHelper.getInstance(mContext);
mDb= mDbHelper.getReadableDatabase();
} /**
* 建立表
* SQLite内部只支持NULL,INTEGER,REAL(浮点),TEXT(文本),BLOB(大二进制)5种数据类型
* 建立表成功了,返回true
*/
public boolean executeSql(String sql){
try {
mDb.execSQL(sql);
return true;
}
catch(SQLiteException e){
return false;
}
} public Cursor executeSql(String sql,String[]args){
try {
return mDb.rawQuery(sql, args);
}
catch(SQLiteException e){
e.printStackTrace();
}
return null;
} /**
* 关闭连接
*/
public void closeDBhelper() {
if (mDbHelper != null) {
mDbHelper.close();
}
} }
DatabaseHelper
package com.kale.cursoradapter; import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper{ private static DatabaseHelper mInstance = null;
//数据库名字
public static final String DATABASE_NAME = "SQLite_db";
//版本号
private static final int DATABASE_VERSION = 1;
//建立默认表的语句,必须有_id这个列
private static final String CREAT_TABLE_TABLE_SQL = "create table test_table("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT," + "name TEXT,age INTEGER);"; public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
} public DatabaseHelper(Context context, String name, int version) {
super(context, name, null, version);
} public static synchronized DatabaseHelper getInstance(Context context) {
if (mInstance == null) {
mInstance = new DatabaseHelper(context);
}
return mInstance;
} /*
* 初次使用时创建数据库表
*/
@Override
public void onCreate(SQLiteDatabase db) {
//通过sql语句建立默认表
db.execSQL(CREAT_TABLE_TABLE_SQL);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
/**可以拿到当前数据库的版本信息 与之前数据库的版本信息 如果不同,那么就更新数据库**/ }
//删除数据库
public boolean deleteDatabase(Context context , String databaseName) {
return context.deleteDatabase(databaseName);
}
}
源码下载:http://download.csdn.net/detail/shark0017/8026895
SimpleCursorAdapter和ListView的结合使用的更多相关文章
- Android开发具体解释之ListView具体解释一
列表ListView介绍和实例 1.ListView -- ListActivity -- ListAdapter 2.ArrayAdapter结合ListView进行显示 3.SimpleA ...
- android-基础编程-ListView
ListView主要包括view和数据源.其数据适配器列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter. ListView的没有oom原因.经 ...
- Android-ContentProvider简单的增删改查
注意:在ContentProvider里面写对数据库增删改查的时候,千万不能 db.close(); cursor.close(); 等操作,不然其他应用访问不到数据,也没有必要写isOpen(); ...
- Android SQLite(1)简单示例-增,删,改,查
1.主要核心类,Sqlite编程要继承SQLiteOpenHelper import android.content.Context; import android.database.sqlite.S ...
- Andriod学习笔记2:“Your content must have a ListView whose id attribute is 'android.R.id.list'”问题的解决办法
问题描述 activity_main.xml代码如下: <?xml version="1.0" encoding="utf-8"?> <Lin ...
- Android软件开发之ListView 详解【转】
ListView的使用方法 ListView是Android软件开发中非常重要组件之一,基本上是个软件基本都会使用ListView ,今天我通过一个demo来教大家怎么样使用ListView组件 绘 ...
- 安卓 android ListView 数据填充
在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示.抽空把对ListView的使用做了整理,并写了个小例子,如下图. 列表的显示需要三 ...
- Android Listview
方法一: xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...
- Android之listview && adapter
今天我们讲的也是非常重要的一个控件listview-最常用也是最难的 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView ...
随机推荐
- 剑指offer题目解答合集(C++版)
数组中重复的数字 二维数组中查找 字符串 替换空格 二叉树的编码和解码 从尾到头打印链表 重建二叉树 二叉树的下一个节点 2个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...
- [java,2017-05-15] 内存回收 (流程、时间、对象、相关算法)
内存回收的流程 java的垃圾回收分为三个区域新生代.老年代. 永久代 一个对象实例化时 先去看伊甸园有没有足够的空间:如果有 不进行垃圾回收 ,对象直接在伊甸园存储:如果伊甸园内存已满,会进行一次m ...
- springboot常见 10问
1.什么是Spring Boot? 多年来,随着新功能的增加,spring变得越来越复杂.只需访问https://spring.io/projects页面,我们就会看到可以在我们的应用程序中使用的所有 ...
- !!!常用SVG代码
http://www.w3school.com.cn/svg/svg_examples.asp svg实例 http://www.w3school.com.cn/svg/svg_reference.a ...
- Optional 的基本用法
参考: https://www.cnblogs.com/xingzc/p/5778090.html http://www.runoob.com/java/java8-optional-class.ht ...
- 为何要使用ViewModel
ViewModel类是用来存储和管理与UI相关的数据,在设计之初就考虑到生命周期的影响.ViewModel允许数据在屏幕旋转等配置变化后存活. Android framework管理UI控制器(如Ac ...
- 安装jdk1.9后报 Error:java: 无效的源发行版: 1.9
现象: intillj IDE 运行main方法 Information:javac 1.8.0_101 was used to compile java sources Error:java: 无效 ...
- leetcode10
class Solution { public boolean isMatch(String s, String p) { if (s == null || p == null) { return f ...
- canvas 2.0 图片绘制
绘制图片drawImage 2013.02.21 by 十年灯·一条评论 本文属于<html5 Canvas画图系列教程> 这里的绘制图片是指把一张现成的图片,绘制到Canvas上面. 有 ...
- netbeans 正则替换
单引号替换为双引号: tablename1('xhw_aa') tablename2('xhw_bb') tablename3('xhw_cc') tablename4('xhw_dd') (t ...