开发者

How to have contact's selected return just the selected contact and not all?

开发者 https://www.devze.com 2023-04-13 07:12 出处:网络
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.

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 onActivityR开发者_如何学Goesult(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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号