本示例实现一个英文词典,核心部分就是打开数据库和查询单词,通过openDatabase()方法来实现,另外openDatabase()方法还实现从res\raw目录复制数据库文件到/sdcard/dictionary目录,复制数据实际上先读取,再写入数据的过程
该方法的具体代码如下:
privata sqliteDatabase openDatabase() {
try{
String databasefilename = DATABASE_PATH + "/" + DATABASE_filename;
// /sdcard/dictionary 目录中没有dictionary.db文件时,将res\raw目录中的数据文件复制到该目录
if(!(new file(databasefilename).exists())) {
inputStream is = getResources().openRawResource(R.raw.dictionary); //定义读取流
fileOutputStream fos = new fileOutputStream(databasefilename);
byte[] buffer = new byte[8192];
int count = is.read(buffer);
while(count > 0)
{ fos.write(buffer,count); }
fos.close();
is.close();
}
// 打开数据库
sqliteDatabase database = sqliteDatabaset.openorCreateDatabase(databasefilename,null);
return database;
}catch(Exception e){}
return null;
}
要监视autoCompleteTextVIEw 组件输入字符的变化,在每输入一个字符时就查询当前输入的字符串开头的英文词典,
为了监视autoCompleteTextVIEw输入,需要实现TextWatcher接口,并在该接口的afterTextChanged() 中编写如下代码:
public voID afterTextChanged(Editable s) {
Cursor cursor = database.rawquery("select english as _ID from t_words where english like ?",new String[]{s.toString() + "%"});
DictionaryAdapter dictionaryAdapter = new DictionaryAdapter(this,cursor,true);
actvWord.setAdapter(dictionaryAdapter); }
如图
具体代码请参见 ch06_dictionary 工程
总结以上是内存溢出为你收集整理的英文词典, 以AutoCompleteTextView输入内容到SQLite数据库查找匹配的单词全部内容,希望文章能够帮你解决英文词典, 以AutoCompleteTextView输入内容到SQLite数据库查找匹配的单词所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)