|
package org.hxsd.events;
import static android.provider.BaseColumns._ID;
import static org.hxsd.events.Constants.TABLE_NAME;
import static org.hxsd.events.Constants.TIME;
import static org.hxsd.events.Constants.TITLE;
import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
public class Events extends ListActivity {
private EventsData events;
//查詢的欄位
private static String[] FROM = {_ID, TIME, TITLE};
//排序
private static String ORDER_BY = TIME + " DESC";
//顯示
private static int[] TO = {R.id.rowid, R.id.time, R.id.title};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
events = new EventsData(this);
try{
//添加記錄
addEvent("Hello, Android!");
//獲得記錄資訊
Cursor cursor = getEvents();
//顯示記錄資訊
showEvents(cursor);
}finally{
//關閉資料庫
events.close();
}
}
/**
* 添加資料
* @param string
*/
private void addEvent(String string){
SQLiteDatabase db = events.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TIME, System.currentTimeMillis());
values.put(TITLE, string);
db.insertOrThrow(TABLE_NAME, null, values);
}
/**
* 獲得記錄
* @return
*/
private Cursor getEvents(){
SQLiteDatabase db = events.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
startManagingCursor(cursor);
return cursor;
}
/**
* 顯示記錄
* @param cursor
*/
private void showEvents(Cursor cursor){
/*StringBuilder builder = new StringBuilder("Saved events:n");
while(cursor.moveToNext()){
long id = cursor.getLong(0);
long time = cursor.getLong(1);
String title = cursor.getString(2);
builder.append(id).append(": ");
builder.append(time).append(": ");
builder.append(title).append("n");
}
TextView textView = (TextView) findViewById(R.id.TextView01);
textView.setText(builder);*/
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);
setListAdapter(adapter);
}
}
EventsData.java檔案大致如下:
package org.hxsd.events;
import static android.provider.BaseColumns._ID;
import static org.hxsd.events.Constants.TABLE_NAME;
import static org.hxsd.events.Constants.TIME;
import static org.hxsd.events.Constants.TITLE;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class EventsData extends SQLiteOpenHelper{
//資料庫名稱
private static final String DATABASE_NAME = "events.db";
//資料庫版本
private static final int DATABASE_VERSION = 2;
//建構函式
public EventsData(Context ctx){
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 建立資料表events
db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + TIME + " INTEGER," + TITLE + " TEXT NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//如果版本有改動,刪除表events然後建立
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
} |