0

How to get contactname from phonenumber please help me Code i used:

public String contactname(String phonenumber)
{
  ContentValues contentValues = new ContentValues();

      Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, 

            phonenumber);
Cursor cur = managedQuery(contactUri, null, null, null, null);
 int nameColumn = cur.getColumnIndex(People.NAME); 
String name = cur.getString(nameColumn);

return name;
}


It shows error
user933909
  • 331
  • 2
  • 4
  • 12
  • [look at this question](http://stackoverflow.com/questions/2174048/how-to-look-up-a-contacts-name-from-their-phone-number-on-android) – confucius Sep 08 '11 at 02:32

1 Answers1

0

the type Contacts.People.Phones is deprecated, i think you should directly use Contacts, Beside, the string you set phonenumber is only used to match various parts of contact name. maybe you can query all of the contacts first, and then match the one by giving the phonenumber.

Uri uri = Contacts.CONTENT_URI;
Cursor cursor = managedQuery(uri, null, null, null, null);
cursor.moveToFirst();
String name = null;
while (curcor.getPosition != cursor.getCount) {
    if (cursor.getString(cursor.getColumnIndex("default_tel")).equals(phonenumber)) {
        name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
        break;
    }
}
return name;
sara
  • 3
  • 3