android自带的AutoCompleteTextView只能从首字母开始匹配,不能灵活的匹配任意字符。比如有如下需求:
输入公司名时,采用智能输入法,根据输入的全称,简称,简拼,编号等自动显示匹配的公司全称。
AutoCompleteTextView就实现不了了。这时候需要自定义AutoComplateTextView的适配器。
AutoCompleteTextView companyAutoCompleteTextView;
AutoCompleteAdater cursorAdapter = new AutoCompleteAdater(this, R.layout.list_item_simple, null, CompanyDb.KEY_FULL_NAME, R.id.text1);
// 设置输入一个字符就弹出提示列表(默认输入两个字符时才弹出提示)
companyAutoCompleteTextView.setThreshold(1);
companyAutoCompleteTextView.setAdapter(cursorAdapter);
其中list_item_simple.xml是自定义的布局,用于下列列表的显示。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:padding="10dp"
android:textColor="#000000"
android:textSize="14sp" >
</TextView>
适配器:AutoCompleteAdapter.java
public class AutoCompleteAdater extends SimpleCursorAdapter {
private CompanyDb dbHelper = null;
private Context context;
// 查询字段
private String queryField;
@SuppressWarnings("deprecation")
public AutoCompleteAdater(Context context, int layout, Cursor c, String from, int to) {
super(context, layout, c, new String[] { from }, new int[] { to });
this.context = context;
this.queryField = from;
}
/**
* 动态查询数据库
*/
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (constraint != null) {
return getDbHelper().query((String) constraint);
} else {
return null;
}
}
/**
* 这里设置在弹出的提示列表中点击某一项后的返回值,返回值将被显示在文本框中
*/
@Override
public CharSequence convertToString(Cursor cursor) {
return cursor.getString(cursor.getColumnIndex(queryField));
}
public CompanyDb getDbHelper() {
if (dbHelper == null) {
dbHelper = new CompanyDb(this.context);
dbHelper.open();
}
return dbHelper;
}
}
public class CompanyDb {
//用于打印log
//private static final String TAG = "PersonalityDataBaseAdapter";
public static final String TABLE_Company_INFO = "company_info";
public static final String KEY_ID = "_id";//表中一条数据的id
public static final String KEY_FULL_NAME = "name";
public static final String KEY_ABBR_NAME = "brename";//简称
public static final String KEY_ABBR_SPELL = "jianpin";//简拼
public static final String KEY_SERIAL = "code";//编号
//本地Context对象
private Context mContext = null;
//执行open()打开数据库时,保存返回的数据库对象
private SQLiteDatabase mSQLiteDatabase = null;
//由SQLiteOpenHelper继承过来
private UserDBHelper mDatabaseHelper = null;
//构造函数
public CompanyDb(Context context)
{
mContext = context;
}
//打开数据库,返回数据库对象
public void open() throws SQLException
{
try {
mDatabaseHelper = new UserDBHelper(mContext);
mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//关闭数据库
public void close()
{
if (mDatabaseHelper != null) {
mDatabaseHelper.close();
}
}
...
}
</pre>android自带的<span style="color:rgb(51,51,51); font-family:Arial; line-height:26px">AutoCompleteTextView只能从首字母开始匹配,不能灵活的匹配任意字符。比如有如下需求:</span><p></p><p><span style="font-family:Arial; line-height:26px; font-size:18px"><span style="color:rgb(51,51,51); white-space:pre"></span><em><span style="color:#cc0000">输入公司名时,采用智能输入法,根据输入的全称,简称,简拼,编号等自动显示匹配的公司全称。</span></em></span></p><p><span style="color:rgb(51,51,51); font-family:Arial; line-height:26px; font-size:18px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:18px; line-height:26px">AutoCompleteTextView就实现不了了。</span></span></p><p></p><p>这时候需要自定义AutoComplateTextView的适配器。</p><p></p><pre name="code" class="java">AutoCompleteTextView companyAutoCompleteTextView;