讲到这里我们应该了解一个 dictionary.db 中的 t_words 表的结果,该表只有两个字段: english 和 chinese 。分别表示单词的英文和中文描述。如果要获得单词的中文描述,只需要查找 chinese 字段即可。如 onClick 方法中的代码所示。
查询单词的效果如图 3 所示。
图 3 查询英文单词
如果显示以输入字符串开头的单词列表
虽然到目前为止,我们的英文词典已经可以正常工作了,但为了方便读者使用,在本节将添加单词输入的自动提示功能。也就是说,如果读者在 AutoCompleteTextView组件中输入单词的前几个字母,该组件就会自动列出数据库中所有以该字符串开头的单词。效果如图2所示。拥有这样 的功能就可以使用户在只知道单词的前几个字母时也可以查找到相应的单词。
由于AutoCompleteTextView组件使用了自定义的Adapter类,下面先给出这个自定义的Adapter类的完整代码。
{
private LayoutInflater layoutInflater;
@Override
public CharSequence convertToString(Cursor cursor)
{
return cursor == null ? "" : cursor.getString(cursor
.getColumnIndex( " _id " ));
}
// 用于将_id字段(也就是english字段)的值设置TextView组件的文本
// view参数表示用于显示列表项的TextView组件
private void setView(View view, Cursor cursor)
{
TextView tvWordItem = (TextView) view;
tvWordItem.setText(cursor.getString(cursor.getColumnIndex( " _id " )));
}
@Override
public void bindView(View view, Context context, Cursor cursor)
{
setView(view, cursor);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
View view = layoutInflater.inflate(R.layout.word_list_item, null );
setView(view, cursor);
return view;
}
public DictionaryAdapter(Context context, Cursor c, boolean autoRequery)
{
super (context, c, autoRequery);
// 通过系统服务获得LayoutInflater对象
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
}
在编写 DictionaryAdapter 类时应注意如下 3 点:
1. 为了将 Cursor 对象与 AutoCompleteTextView 组件绑定, DictionaryAdapter 类必须从 CursorAdapter 类继承。
2. 由于 CursorAdapter 类中的 convertToString 方法直接返回了 Cursor 对象的地址,因此,在 DictionaryAdapter 类中必须覆盖 convertToString 方法,以返回当前选中的单词。 CursorAdapter 类中的 convertToString 方法的源代码。
{
// 如果cursor不为null,返回Cursor对象的地址(cursor.toString())
return cursor == null ? "" : cursor.toString();
}
覆盖后的 convertToToString 方法的源代码如下:
{
return cursor == null ? "" : cursor.getString(cursor
.getColumnIndex( " _id " ));
}
在这里要注意一下,当选中 AutoCompleteTextView 组件中单词列表中某一个单词后,系统会用 convertToString 方法的返回值来设置 AutoCompleteTextView 组件中的文本。因此,必须使用 Cursor 的 getString 来获得相应的字段值。
3. 由于将 Cursor 对象与 Adapter 绑定时必须要有一个叫“ _id ”的字段,因此,在本例中将 english 字段名映射成了“ _id ”字段。
为了监视 AutoCompleteTextView 组件中的文本输入情况,需要实现 android.text.TextWatcher 接口。在该接口中只需要实现 afterTextChanged 方法即可,代码如下:
{
// 必须将english字段的别名设为_id
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是在Main类中定义的AutoCompleteTextView类型的变量
actvWord.setAdapter(dictionaryAdapter);
}
从上面的代码中可以看到,在查询 SQL 语句中的 english 字段名的别名是“ _id ”。
4. 在 DictionaryAdapter 类中需要使用 bindView 和 newView 方法设置每一个列表项。 bindView 方法负责设置已经存在的列表项,也就是该列表项已经生成了相应的组件对象。而 newView 方法负责设置新的列表项,在该方法中需要创建一个 View 对象来显示当前的列表项。在本例中使用 word_list_item.xml 布局文件来显示每一个列表项,代码如下:
< TextView xmlns:android ="http://schemas.android.com/apk/res/android"
android:id ="@+id/tvWordItem"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:textAppearance ="?android:attr/textAppearanceLarge"
android:gravity ="center_vertical"
android:paddingLeft ="6dip"
android:textColor ="#000"
android:minHeight ="?android:attr/listPreferredItemHeight"
/>
本文介绍了实现基于 Android 的英文词典的实现方法。实现英文词典主要需要解决 3 个问题:如何将保存英文单词的 SQLite 数据库文件随同 apk 文件一起发布;如何打开 SD 卡中的数据库文件;如何在 AutoCompleteTextView 组件显示以输入字符串开头的英文单词列表。在最后仍然要提一句的是在编写自定义 DictionaryAdapter 类时一定要覆盖 contertToString 方法,以便在用户选项某一个列表项时在 AutoCompleteTextView 组件中显示选中的单词,而不是 Cursor 对象地址。
《Android/OPhone开发完全讲义》(本书版权已输出到台湾)
样章和目录下载
互动网 当当网 卓越亚马逊
《人人都玩开心网:Ext JS+Android+SSH整合开发Web与移动SNS》
样章下载
互动网
乐博Android手机客户端(新浪微博) 发布