0

I am trying to make an application on android that takes the contact name as a string input and returns his phone number if that contact exists in the phone book...

I tried searching around but there is no clear tutorial as to how to do exactly that

input:contact name outputs:the phone number

please help

      Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 


     while (cursor.moveToNext()) { 
         String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

         if(name.equalsIgnoreCase(token3)) {

        try{     ContentResolver cr = context.getContentResolver();
             Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { ContactsContract.CommonDataKinds.Phone._ID}, null);
             String lname = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

         Toast.makeText(context, "number is "+lname, Toast.LENGTH_LONG).show();
        }catch (Exception e) {
            // TODO: handle exception
        }

         }
           } 

it's what I have so far. the piece of code in the try catch block always crashes.

3 Answers 3

3

I wrote this method eventually to solve my problem

public String get_Number(String name,Context context)

{String number="";


Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER};

Cursor people = context.getContentResolver().query(uri, projection, null, null, null);

int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

people.moveToFirst();
do {
    String Name   = people.getString(indexName);
    String Number = people.getString(indexNumber);
    if(Name.equalsIgnoreCase(name)){return Number.replace("-", "");}
    // Do work...
} while (people.moveToNext());


if(!number.equalsIgnoreCase("")){return number.replace("-", "");}
else return number;
}

it may not be very efficient but hey it works

2

Try this way...

Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,);
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(name.equals(Your_String)) {
    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
    String lname = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
1
  • it contains some synthax errors and stuff. what is cr supposed to be? Jun 25, 2012 at 11:22
1

Try this way..

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER};

            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

            int idxName = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int idxNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

            if(cursor.moveToFirst()) {
                do {
                    contactname   = cursor.getString(idxName);
                    contactNumber = cursor.getString(idxNumber);

                    if (contactname.equals("YOUR CONTACT NAME")){
                        Log.d(LOG_TAG,"Contact Name -> "+ contactname +" Contact Number -> "+ contactNumber);
                    }
                } while (cursor.moveToNext());
            }
            cursor.close();
2
  • You're only 6 years late :D Apr 22, 2018 at 19:22
  • 3
    :) I thought it will be helpful for other programmer. Apr 23, 2018 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.