booleanmoveToNext()同moveToPosition(position)
4、SimpleCursorAdapter(android.widget.SimpleCursorAdapter)
也许你会奇怪了,之前我还说过关于数据库的操作都在database和database.sqlite包下,为什么把一个Adapter放到这里,如果你用过
Android
的SQLite3,你一定会知道
,这是因为我们对数据库的操作会经常跟列表联系起来
经常有朋友会在这出错,但其实也很简单
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.list,
myCursor,
new String[] {DB.TEXT1,DB. TEXT2},
new int[]{ R.id.list1,R.id.listText2 });
my.setAdapter(adapter);
一共5个参数,具体如下:
参数1:Content
参数2:布局
参数3:Cursor游标对象
参数4:显示的字段,传入String[]
参数5:显示字段使用的组件,传入int[],该数组中是TextView组件的id
到这里,关于数据库的操作就结束了,但是到目前为止我只做了翻译的工作,有些同学可能还是没有掌握,放心,下面我们一起顺着正常开发的思路理清一下头绪!
前面的只是帮没做过的朋友做下普及,下面才是你真正需要的!
一、写一个类继承SQLiteOpenHelpe
public class DatabaseHelper extends SQLiteOpenHelper
构造方法:
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
在onCreate方法里写建表的操作
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE tb_test (_id INTEGER DEFAULT '1' NOT NULL PRIMARY KEYAUTOINCREMENT,class_jb TEXTNOT NULL,class_ysbj TEXTNOTNULL,title TEXTNOT NULL,content_ysbj TEXTNOT NULL)";
db.execSQL(sql);//需要异常捕获
}
在onUpgrade方法里删除现有表,然后手动调用onCtreate创建表
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "drop table "+tbname;
db.execSQL(sql);
onCreate(db);
}
对表增、删、查、改的方法,这里用的是SQLiteOpenHelper提供的方法,也可以用sql语句实现,都是一样的
关于获取可读/可写SQLiteDatabase,我不说大家也应该会想到,只有查找才会用到可读的SQLiteDatabase
/**
* 添加数据
*/
public long insert(String tname, int tage, String ttel){
SQLiteDatabase db= getWritableDatabase();//获取可写SQLiteDatabase对象
//ContentValues类似map,存入的是键值对
ContentValues contentValues = new ContentValues();
contentValues.put("tname", tname);
contentValues.put("tage", tage);
contentValues.put("ttel", ttel);
return db.insert(tbname, null, contentValues);
}
/**
* 删除记录
* @param _id
*/
public void delete(String _id){
SQLiteDatabase db= getWritableDatabase();
db.delete(tbname,
"_id=?",
new String[]{_id});
}
/**
* 更新记录的,跟插入的很像
*/
public void update(String _id,String tname, int tage, String ttel){
SQLiteDatabase db= getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("tname", tname);
contentValues.put("tage", tage);
contentValues.put("ttel", ttel);
db.update(tbname, contentValues,
"_id=?",
new String[]{_id});
}
/**
* 查询所有数据
* @return Cursor
*/
public Cursor select(){
SQLiteDatabase db = getReadableDatabase();
return db.query(
tbname,
new String[]{"_id","tname","tage","ttel","taddr"},
null,
null, null, null, "_id desc");
}
关于db.query方法的参数,有很多,为了防止大家弄乱,我简单说一下
参数1:表名
参数2:返回数据包含的列信息,String数组里放的都是列名
参数3:相当于sql里的where,sql里where后写的内容放到这就行了,例如:tage>?
参数4:如果你在参数3里写了?(知道我为什么写tage>?了吧),那个这里就是代替?的值 接上例:new String[]{"30"}
参数5:分组,不解释了,不想分组就传null
参数6:having,想不起来的看看SQL
参数7:orderBy排序
到这里,你已经完成了最多的第一步!我们来看看都用到了那些类:
SQLiteOpenHelper我们继承使用的
SQLiteDatabase增删查改都离不开它,即使你直接用sql语句,也要用到execSQL(sql)
二、这里无非是对DatabaseHelper类定义方法的调用,没什么可说的,不过我还是对查询再唠叨几句吧
Android
查询出来的结果一Cursor形式返回
cursor = sqLiteHelper.select();//是不是很简单?
查询出来的cursor一般会显示在listView中,这就要用到刚才提到的SimpleCursorAdapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.list_row,
cursor,
new String[]{"tname","ttel"},
new int[]{R.id.TextView01,R.id.TextView02}
);