I want to show the contacts stored in android mobile while typing text in a EditText box. I want to display the contacts below the EditText box like the messaging screen. I can retrieve the contacts using ContactsContract, but I don’t know how to display like the messaging screen in Android. Does anybody know something about this?
My current code:
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
System.out.println("contactsID-->>>"+id);
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
System.out.println("contactsName-->>>"+name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(Contacts.Phones.CONTENT_URI, null,
Contacts.Phones.PERSON_ID +" = ?",
new String[]{id}, null);
int i=0;
int pCount = pCur.getCount();
while (pCur.moveToNext()) {
String phoneNum = pCur.getString(
pCur.getColumnIndex(Contacts.Phones.NUMBER));
System.out.println("PhoneNum-->>>"+phoneNum);
}
// Query phone here. Covered next
}
}
}
You need to use AutoCompleteTextView and use the contact names read from ContactsContract to populate the adapter. Code: