0
ContentResolver cr = getContentResolver();
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));
        String name = cur.getString(
        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        if (name.equals(selected) && Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

            Cursor pCur = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " =?", new String[] {
                id
            }, null);
            finalsend = ContactsContract.CommonDataKinds.Phone.NUMBER;
            while (pCur.moveToNext()) {

            }
            pCur.close();

        }

The code is supposed to look through contacts to find one that matches the "selected" variable which appears to work fine but then it is supposed to switch to the ContactsContract.CommonDataKinds.Phone.CONTENT_URI and find the same contact by matching up the ID's and give me the phone number saved for that contact.

Every time it returns "Data1", what am I doing wrong? It's probably a stupid mistake but any help is appreciated.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
adoo42
  • 9
  • 2

1 Answers1

0

Following code snippet is working for me:

String id , name;
ContentResolver cr = getContentResolver();

     String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

     Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,  null, null, null, sortOrder);



      if (cur.getCount() > 0) {


            while (cur.moveToNext()) {



                id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

                name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                Log.i(tag, "Id is "+ id+"\t Name is"+name);

            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){                      


                        Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

                            null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);



                        while (pCur.moveToNext()) {


                // Do something with phones



                       int phNumber = pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);

                       String phn = pCur.getString(phNumber);

                       Log.i("phn number",  phn);



                    } 

                    pCur.close();

                }

                }

    }
Pawan
  • 1,503
  • 2
  • 19
  • 28
  • Thanks so much. the part I was missing was `int phNumber = pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);` – adoo42 Nov 18 '11 at 18:14