1

I am using this code to return the contacts a user has. But i want to tranform this code to only display the contacts selected in the Contact selection.

 @Override  
public void onActivityResult(int reqCode, int resultCode, Intent data) {  
    super.onActivityResult(reqCode, resultCode, data); 
    ContentResolver cr = getContentResolver();
    getContacts(cr);
}

public static void getContacts(ContentResolver cr) {
    String number = null;
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
        // read id
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            /** read names **/
            String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            /** Phone Numbers **/
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                   number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String typeStr = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
            }
            pCur.close();
         Log.e("NAME", displayName + number);
        }

    }
    }

}

How would i transform this code to have it return the contact selected and instead of all the contacts?

coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118

2 Answers2

1

You may also be able to use the example included in response to this older post on SO How to call Android contacts list?. SQRFV's point is well taken in that we're missing a layout context for the user to have the option to see and select the contact in the first place.

Community
  • 1
  • 1
Carth
  • 2,303
  • 1
  • 17
  • 26
0

Where are you storing the selected contact? This method simply looks through the list of contacts on the phone - there is no notion of a stored or menu value. What is the layout code that you are using for this activity?

If you have the contact from the layout, or you were passed the contact via some other method, you can do a direct query on the phone to retrieve this data. See: http://developer.android.com/resources/articles/contacts.html, especially the Loookup URI section.

Noah
  • 1,966
  • 1
  • 14
  • 29
  • All i want to do is on activity result i want to return the selected contact. Everything ive been given or tried doesnt seem to work. – coder_For_Life22 Oct 14 '11 at 18:52
  • The tricky thing about android is that the java code is just 1/2 the equation. How are we getting to the method you posted? What is the layout that the user is seeing when you are doing this work behind the scenes? – Noah Oct 14 '11 at 18:57