SimpleCursorAdapter類

SimpleCursorAdapter類構造方法的定義

public SimpleCursorAdapter(Context context,int layout,Cursor c,String[]from,int[] to)

SimpleCursorAdapter類將一個數據表綁定在ListView上,也就是說,該ListView會顯示數據表的全部記錄.在綁定數據之前,需要先編寫一個SQLiteOpenHelper類的子類,用於運算元據庫,代碼如下

SimpleCursorAdapter類構造方法的定義

publicSimpleCursorAdapter(Contextcontext,intlayout,Cursorc,String[]from,int[]to)
SimpleCursorAdapter類將一個數據表綁定在ListView上,也就是說,該ListView會顯示數據表的全部記錄.在綁定數據之前,需要先編寫一個SQLiteOpenHelper類的子類,用於運算元據庫,代碼如下

package net.blogjava.mobile.db;

import java.util.Random;

import android.content.Context;

import andorid.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class DBService extends SQLiteOpenHelper

{

private final static int DATABASE_VERSION = 1;

private final static int DATABASE_NAME = "test.db";

@Override

public void OnCreate(SQLiteDatabase db)

{

String sql = "CREATE TABLE[t_test]("+"[_id]AUTOINC,"

+"[name]VARCHAR(20) NOT NULL ON CONFLICT FAIL,"

+ "CONSTRAINT[sqlite_autoindex_t_test_1] PRIMARY KEY ([_id]))";

db.execSQL(sql);

Random random = new random();

for(int i = 0;i<20;i++)

{

String s = "";

//隨機生成長度為10的字元串

for(int j=0;j<10;j++)

{

char c = (char) (97 + random.nextInt(26));

s += c;

}

//執行insert語句

db.execSQL("insert into t_test(name) values(?)",new Object[]{s});

}

}

public DBService(Context context)

{

super(context,DATABASE_NAME,null,DATABASE_VERSION);

}

//由於不打算對test.db進行升級,因此,在該方法中沒有任何代碼

@Override

public void onUpgrade(SQLiteDatabase db,int oldVersion,int new Version)

{

}

//執行select語句

public Cursor query(String sql,String[] args)

{

SQLiteDataBase db = this.getReadableDatabase();

Cursor cursor = db.rawQuery(sql,args);

return cursor;

}

}

相關詞條

相關搜尋

熱門詞條

聯絡我們