Reputation: 70416
I have some contacts autocomplete and autosearch algorithm working for my android app. First some xml to define the text view for the input:
<AutoCompleteTextView
a:id="@+id/recipientBody"
a:layout_width="0dip"
a:layout_height="wrap_content"
a:layout_weight="1.0"
a:nextFocusRight="@+id/smsRecipientButton"
a:hint="@string/sms_to_whom"
a:maxLines="10"
/>
And now I setup the text view
AutoCompleteTextView recip =
(AutoCompleteTextView) findViewById(R.id.recipientBody);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.list_item, getAllContacts());
recip.setAdapter(adapter);
And now the actual algorithm that searches for a contact that matches the input:
private List<String> getAllContacts() {
List<String> contacts = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{contactId}, null);
while (pCursor.moveToNext()) {
String phoneNumber = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts.add(phoneNumber + " ( " + displayName + " )");
}
pCursor.close();
}
}
}
return contacts;
}
This works fine for both contact number and name input. But there is still a problem. The user can input multiple phone numbers. But when one contact is applied to the text view it cannot search again, because the algorithm takes the whole string.
How can I solve that?
Upvotes: 1
Views: 1195
Reputation: 1842
EDIT:
Well, I thought about it for a while and spotted a problem with my solution - there's no place where you could insert the contact from the completion list into the TextView.
The solution seems to be MultiAutoCompleteTextView, this thing is designed for solving your problem.
Sorry for confusion!
For me, it looks like you need a custom adapter.
You may extend ArrayAdapter<String>
and implement getFilter()
- of course you will also need a custom filter (extending Filter
) which instance you will return from that method.
Filter's performFiltering
method has one parameter - the string for which the list of suggestion is needed. You need to take the part after the last comma (or whatever character are you using as a separator) and return the suggestion list for that substring.
P.S.
For the better user experience, you may also think of styling your AutoCompleteTextView contents with Spans: http://ballardhack.wordpress.com/2011/07/25/customizing-the-android-edittext-behavior-with-spans/
Upvotes: 1