標籤:android notes sqlite
先附上原始碼。代碼不斷更新中,主要實現一個記事本的功能,涉及SQLite的CRUD操作。
dummynote.java
package com.example.dummynote;import android.os.Bundle;import android.support.v4.widget.SimpleCursorAdapter;import android.view.Menu;import android.view.MenuItem;import android.R.bool;import android.app.ListActivity;import android.database.Cursor;import android.widget.ArrayAdapter;import android.widget.ListAdapter;public class DummyNote extends ListActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);getListView().setEmptyView(findViewById(R.id.empty));//設定列表為空白時的顯示內容為 empty文字框setAdapter();}private String[] note_array={/*"hangsome","lushichuanshuo","android","library"*/};private NotesDbAdapter mDbHelper ;private Cursor mNoteCursor;private void setAdapter(){mDbHelper = new NotesDbAdapter(this);mDbHelper.open();fillData();}private void fillData(){mNoteCursor = mDbHelper.getall();startManagingCursor(mNoteCursor);String[] from = new String[]{NotesDbAdapter.NOTE};int[] to = new int[]{android.R.id.text1};//android.R.id.text1是Android 架構裡面的TextView的一個標識符//Now create a simple cursor adapterSimpleCursorAdapter adapter =new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, mNoteCursor, from, to);setListAdapter(adapter);}//Add a entityprivate int mNoteNumber = 1;protected static final int MENU_INSERT = Menu.FIRST;@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. super.onCreateOptionsMenu(menu); menu.add(0,MENU_INSERT, 0, "新增記事");return super.onCreateOptionsMenu(menu);}public boolean onOptionsItemSelected(MenuItem item){switch(item.getItemId()){case MENU_INSERT:String noteName = "Note "+ mNoteNumber++;mDbHelper.create(noteName);fillData();return true;}return super.onOptionsItemSelected(item);}}<p></p>
NotesDbAdapter.java<pre name="code" class="java">package com.example.dummynote;import java.util.Date;import android.content.ContentValues;import android.content.Context;import android.database.SQLException;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.database.*;import android.util.Log;public class NotesDbAdapter {private static final String TAG = "notes";private static final String DATABASE_NAME = "notes.db";private static final int DATABASE_VERSION = 1 ;private static final String DATABASE_TABLE = "notes";private static final String DATABASE_CREATE = "create table notes("+"_id INTEGER PRIMARY KEY,"+"note TEXT NOT NULL,"+"created INTEGER"+");";private static class DatabaseHelper extends SQLiteOpenHelper{public DatabaseHelper(Context context) {super(context, DATABASE_NAME,null,DATABASE_VERSION);// TODO Auto-generated constructor stub}@Override//建立資料表public void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubtry {db.execSQL(DATABASE_CREATE);Log.d(TAG, "onCreate !");} catch (Exception e) {Log.d(TAG, e.toString());}}@Override//更新資料表public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {try {// TODO Auto-generated method stubdb.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);onCreate(db);Log.d(TAG, "inUpgrade!");} catch (Exception e) {Log.d(TAG, "onUpgrade failure!");}}}private Context mCtx = null; //抽象介面private DatabaseHelper dbHelper; //資料庫工具類private SQLiteDatabase db; //資料庫類/** COnstructor**/public NotesDbAdapter(Context ctx){this.mCtx=ctx;}public NotesDbAdapter open () throws SQLException{dbHelper = new DatabaseHelper(mCtx);db = dbHelper.getWritableDatabase();//資料庫不存在就創造一個,若存在就根據版本庫來決定是否更新資料庫return this;}public void close(){dbHelper.close();}public static final String ROWID = "_id";public static final String NOTE = "note";public static final String CREATED = "created";String[] strCols = new String[]{ROWID,NOTE,CREATED};//檢索。返回的是指標。少記憶體public Cursor getall() {return db.query(DATABASE_TABLE,//Which table to selectstrCols,//Which columns to returnnull, //Where clausenull, //Where argumentsnull, //Group By clausenull, //Having clausenull //Order-by clause);}//增加public long create(String Note){Date now = new Date();ContentValues args = new ContentValues();args.put(NOTE, Note);args.put(CREATED, now.getDate());return db.insert(DATABASE_TABLE, null, args);}}
SQLite執行個體——dummynote.java