一.提要
英文詞典是手機中經常使用的應用。因此,在本文將結合 Android 來討論如何實現一個 Android 版的英文詞典。實現英文詞典的方法很多。在本文使用了 SQLite 數據庫來保存英文單詞信息。系統通過 SQLite 數據庫中保存的單詞信息來查找到與指定英文對應的中文信息。當然,實現這樣一個英文詞典需要解決一系列技術問題。例如,如何將保存英文單詞信息的數據庫文件隨程序( apk 文件)一起發布;發布後如何打開數據庫。
先看最終效果:
二. 需要解決的幾個問題
1.外部數據庫的調用
首先得准備好詞典的數據庫文件,沒有的點這裡下載。
在res文件夾中新建raw文件夾,然後將數據庫文件拷貝進去,最終像這樣:
接著編寫初始化函數,在主activy中的OnCreate函數調用就可以了:
- public void init()
- {
-
- try
- {
- // 獲得dictionary.db文件的絕對路徑
- String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
- File dir = new File(DATABASE_PATH);
- // 如果/sdcard/dictionary目錄中存在,創建這個目錄
- if (!dir.exists())
- dir.mkdir();
- // 如果在/sdcard/dictionary目錄中不存在
- // dictionary.db文件,則從res\raw目錄中復制這個文件到
- // SD卡的目錄(/sdcard/dictionary)
- if (!(new File(databaseFilename)).exists())
- {
- // 獲得封裝dictionary.db文件的InputStream對象
- InputStream is = getResources().openRawResource(
- R.raw.dictionary);
- FileOutputStream fos = new FileOutputStream(databaseFilename);
- byte[] buffer = new byte[8192];
- int count = 0;
- // 開始復制dictionary.db文件
- while ((count = is.read(buffer)) > 0)
- {
- fos.write(buffer, 0, count);
- }
-
- fos.close();
- is.close();
- }
-
- }
- catch (Exception e)
- {
- }
- }
2.數據庫的初始化
創建一個DBHelper 繼承SQLiteOpenHelper,來對打開和關閉數據庫。
- package com.example.dictionary;
-
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
-
- public class DBHelper extends SQLiteOpenHelper {
-
- private static final String DATABASE_NAME = "dictionary.db";
- private static final int DATABASE_VERSION = 1;
- private final static String DATABASE_PATH = android.os.Environment
- .getExternalStorageDirectory().getAbsolutePath()
- + "/dictionary";
- public DBHelper(Context context) {
- //CursorFactory設置為null,使用默認值
- super(context, DATABASE_PATH + "/" + DATABASE_NAME, null, DATABASE_VERSION);
- System.out.println("New DBHelper!");
-
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- // TODO Auto-generated method stub
- Log.d("DB", "New DB!");
- System.out.println("New DB!");
- //db.execSQL("CREATE TABLE IF NOT EXISTS thing" + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR, detail VARCHAR,isdone INTEGER)");
-
- }
-
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // TODO Auto-generated method stub
- db.execSQL("ALTER TABLE thing ADD COLUMN other STRING");
- }
-
-
- }
3.再封裝一個DBmanager,來管理數據庫的操作
- package com.example.dictionary;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
-
- public class DBManager {
- private DBHelper helper;
- private SQLiteDatabase db;
-
- public DBManager(Context context) {
- helper = new DBHelper(context);
- System.out.println("New DBManager!");
- //因為getWritableDatabase內部調用了mContext.openOrCreateDatabase(mName, 0, mFactory);
- //所以要確保context已初始化,我們可以把實例化DBManager的步驟放在Activity的onCreate裡
- db = helper.getWritableDatabase();
- }
-
- /**
- * query all persons, return cursor
- * @return Cursor
- */
-
- public Cursor queryTheCursor(String[] word) {
- String sql = "select chinese from t_words where english=?";
- Cursor c = db.rawQuery(sql,word);
- return c;
- }
-
- /**
- * close database
- */
- public void closeDB() {
- db.close();
- }
- }
三.主activity代碼
- package com.example.dictionary;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
-
- public class DBManager {
- private DBHelper helper;
- private SQLiteDatabase db;
-
- public DBManager(Context context) {
- helper = new DBHelper(context);
- System.out.println("New DBManager!");
- //因為getWritableDatabase內部調用了mContext.openOrCreateDatabase(mName, 0, mFactory);
- //所以要確保context已初始化,我們可以把實例化DBManager的步驟放在Activity的onCreate裡
- db = helper.getWritableDatabase();
- }
-
- /**
- * query all persons, return cursor
- * @return Cursor
- */
-
- public Cursor queryTheCursor(String[] word) {
- String sql = "select chinese from t_words where english=?";
- Cursor c = db.rawQuery(sql,word);
- return c;
- }
-
- /**
- * close database
- */
- public void closeDB() {
- db.close();
- }
- }