我正在尝试实施自定义AutoCompleteTextView,用于从显示联系人姓名,电话号码类型和电话号码的建议列表中选择联系人的电话号码.我创建了一个自定义CursorAdapter,它为每个建议定义和设置我的Layout和TextViews,并通过runQueryOnBackgroundThread根据用户输入的文本查询联系人.我遇到的问题是,对于输入的前两个值,建议似乎是正确的(例如"ab"表示"abcd"和"abyz"),但除此之外的任何事情都没有(例如"abc"表示"abyz").对于后者,当选择"abyz"建议时,返回"abcd"的值.
主要活动代码:
final ContactInfo cont = new ContactInfo(ctx);
Cursor contacts = cont.getContacts2(null);
startManagingCursor(contacts);
ContactsAutoCompleteCursorAdapter adapter = new ContactsAutoCompleteCursorAdapter(this, contacts);
mPersonText.setAdapter(adapter);
mPersonText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
mPersonNum.setText(number);
}
});
Run Code Online (Sandbox Code Playgroud)
我的联系人类的代码,它返回所有联系人的游标:
public Cursor getContacts2(String where)
{
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = ctx.getContentResolver().query(uri, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
return …Run Code Online (Sandbox Code Playgroud)