0
此AutoCompleteTextView工作正常,直到我創建了FilterQueryProvider並將其傳遞給我的Adapter的setFilterQueryProvider方法。基本上我想要做的是讓用戶輸入聯繫人的姓名或電話號碼,並顯示相關的聯繫人。這裏是我的相關代碼:帶有CursorAdapter的AutoCompleteTextView適用於虛擬設備,但不適用於物理設備
//Set up the behavior for the recipient field.
AutoCompleteTextView destination = (AutoCompleteTextView) findViewById(R.id.destination_number);
//Get the list of contacts, add it to an array adapter.
/*
ArrayList<String>[] contactNames = getContactList();
ArrayList<String> contactNumbers = getContactNumbers(contactNames[1]);
*/
//Initialize the CursorAdapter.
final ContentResolver cr = getContentResolver();
Cursor nameCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE},
ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "> 0", null, null);
CompleteCursorAdapter mAdapter = new CompleteCursorAdapter(NewMessageActivity.this, nameCursor, false);
nameCursor.close();
FilterQueryProvider filter = new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
String query = "(instr(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +
", '" + constraint + "') > 0 OR instr(" +
ContactsContract.CommonDataKinds.Phone.NUMBER
+ ", '" + constraint + "') > 0) AND " + ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "> 0";
return cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE},
query, null, null);
}
};
mAdapter.setFilterQueryProvider(filter);
destination.setAdapter(mAdapter);
destination.setThreshold(2);
CompleteCursorAdapter只是一個CursorAdapter的一個簡單的擴展版本,到目前爲止它已經工作的罰款。我假設由於我在我正在測試的虛擬設備上只有幾個聯繫人,而在我的實際電話上只有幾百個聯繫人,所以我認爲查詢時間太長了,而且它正在被系統。
但是,即使我在這一思路上是正確的,我不知道我能做些什麼來解決這個問題。任何幫助將非常感激!