Reputation: 111
I`m trying to get all the contact names associated with a given phone number on android.
public void getContactName(Context context, String phoneNumber) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = context.getContentResolver().query(uri, new String[]{
ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
System.out.println("ID : " + contactId + " Name : " + contactName);
}
cursor.close();
}
}
But I`m getting the duplicate values
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3480 Name : Issac
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3480 Name : Issac
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3401 Name : Ann
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 2819 Name : Sam
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3480 Name : Issac
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3480 Name : Issac
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 2819 Name : Sam
Is there anything wrong with the code? How do i get all the contact names by passing a phone number?
Upvotes: 0
Views: 324
Reputation: 111
I really don`t know why the cursor returning same values multiple times.To get the unique values,I stored the ids in arraylist which holds only unique values.Then by looping contact ids i can get the contact details
public void getContactName(Context context, String phoneNumber) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = context.getContentResolver().query(uri,
new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID},null, null, null);
List<String> listIds = new ArrayList<String>(); //Arraylist to hold the unique ids
if (cursor != null) {
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
if(!listIds.contains(contactId))
listIds.add(contactId); //adding unique id to arraylist
}
//pass unique ids to get contact names
for (int i = 0; i < listIds.size(); i++) {
String newId = listIds.get(i);
Cursor cursorDetails = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{newId}, null);
if (cursorDetails != null) {
if (cursorDetails.moveToFirst()) {
String contactName = cursorDetails.getString(cursorDetails.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
System.out.println("ID : " + newId + " Name : " + contactName + " Number : " + phoneNumber);
}
cursorDetails.close();
}
}
cursor.close();
}
}
Logcat output:
10-03 12:24:08.161 6306-6306/? I/System.out: ID : 3480 Name : Issac
10-03 12:24:08.161 6306-6306/? I/System.out: ID : 3401 Name : Ann
10-03 12:24:08.171 6306-6306/? I/System.out: ID : 2819 Name : Sam
Upvotes: 0