public class Dictionary extends Activity implements OnClickListener, TextWatcher{
private final String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/dictionary";
private final String DATABASE_FILENAME = "dictionary.db3";
SQLiteDatabase database;
Button btnSelectWord;
AutoCompleteTextView actvWord;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 打开数据库,database是在Main类中定义的一个SQLiteDatabase类型的变量
database = openDatabase();
// 下面的代码装载了相关组件,并设置了相应的事件
btnSelectWord = (Button) findViewById(R.id.btnSelectWord);
actvWord = (AutoCompleteTextView) findViewById(R.id.actvWord);
btnSelectWord.setOnClickListener(this);
actvWord.addTextChangedListener(this);
}
public void onClick(View view)
{
// 查找单词的SQL语句
String sql = "select chinese from t_words where english=?";
Cursor cursor = database.rawQuery(sql, new String[]
{ actvWord.getText().toString() });
String result = "未找到该单词.";
// 如果查找单词,显示其中文信息
if (cursor.getCount() > 0)
{
// 必须使用moveToFirst方法将记录指针移动到第1条记录的位置
cursor.moveToFirst();
result = cursor.getString(cursor.getColumnIndex("chinese"));
Log.i("tran", "success"+result);
}
// 显示查询结果对话框
new AlertDialog.Builder(this).setTitle("查询结果").setMessage(result)
.setPositiveButton("关闭", null).show();

}

private SQLiteDatabase openDatabase() {
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();
}
// 打开/sdcard/dictionary目录中的dictionary.db文件
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
databaseFilename, null);
return database;
} catch (Exception e) {
}
return null;
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

}

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. android 配置文件
  4. android 查看apk中资源文件
  5. Android(安卓)文件工具FileUtil
  6. Android读取assets目录下所有文件
  7. android怎样在布局文件监听事假
  8. 如何在android中使用你自己的数据文件
  9. Android(安卓)自定义时间选择器

随机推荐

  1. tytytytytyt
  2. 笔记-Android学习历程
  3. android:利用反射查看底层API的支持
  4. android linearlayout
  5. Android的ViewFlipper-android学习之旅(三
  6. Android(安卓)activity exported属性理解
  7. android全屏问题(隐藏虚拟按键)
  8. Linux下配置NDK、JDK环境变量
  9. android在线源码地址
  10. android的小疑点:You need to use a Theme