Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6707773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:40:45+00:00 2026-05-26T07:40:45+00:00

I am using this to return a result from the contact picker. protected void

  • 0

I am using this to return a result from the contact picker.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

     if (resultCode == RESULT_OK) {
     switch (requestCode) {
     case CONTACT_PICKER_RESULT:
     Cursor cursor = null;
     ContentResolver cr = getContentResolver();

     try {
     Uri result = data.getData();
     Log.v(DEBUG_TAG, "Got a contact result: "
                            + result.toString());

        // get the contact id from the Uri
        String id = result.getLastPathSegment();
    cursor =  managedQuery(data.getData(), null, null, null, null);  
    cursor.moveToNext(); 
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));  
                      name=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
                    number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));

       Log.v("ID", contactId + name);

     // query for phone number
     cursor = getContentResolver().query(Phone.CONTENT_URI,
                            null, Phone.CONTACT_ID + "=?", new String[] { id },
                            null);

           int phoneIdx = cursor.getColumnIndex(Phone.DATA);
    int lastNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds
                            .StructuredName.FAMILY_NAME);
     int firstNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds
                            .StructuredName.GIVEN_NAME);

                    // get the phone number
                    if (cursor.moveToFirst()) {
                        number = cursor.getString(phoneIdx);
                        lastName = cursor.getString(lastNameIdx);
                        firstName = cursor.getString(firstNameIdx);


                        Log.v(DEBUG_TAG, "Got number " + number);
                    } else {
                        Log.w(DEBUG_TAG, "No results");
                    }
                } catch (Exception e) {
                    Log.e(DEBUG_TAG, "Failed to get phone number data", e);
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }

                    if (number.length() == 0) {
                        Toast.makeText(this, "No phone number found for this contact.",
                                Toast.LENGTH_LONG).show();
                    }
                    if(lastName.length()==0) {
                        Toast.makeText(this, "No last name found for this contact.", 
                                Toast.LENGTH_LONG).show();
                    }
                    if(firstName.length()==0) {
                        Toast.makeText(this, "No first name found for this contact.", 
                                Toast.LENGTH_LONG).show();
                    }

                }

                break;
            }

        } else {
            Log.w(DEBUG_TAG, "Warning: activity result not ok");
        }

     ContactInfo.setText("Contact Name: "+name+ " Phone Number: "+number);
    }

This method returns that name of the user but it doesnt return the phone number for some reason. How do i go about doing this? My code to get the phone number doesnt seem to be working.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:40:46+00:00Added an answer on May 26, 2026 at 7:40 am

    Try this code, it is working for me

    public static void getContacts(ContentResolver cr) {
    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()) {
                    String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String typeStr = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
            }
            pCur.close();
    
        }
    }
    }
    

    Log the displayName, number, typeStr variables. Enjoy !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using following code but cannot return data from MySQL. This is the output:
I am using this method to return a result code and then get a
I'm using this code to return a FileContentResult with an MSI file for the
Should I be using this method of throwing errors: if (isset($this->dbfields[$var])) { return $this->dbfields[$var];
When I say this using (Entities db = new Entities()) { return db.TableName.AsQueryable().ToList(); }
Why does this script return a pair of nulls? I'm using SQL Server 2008,
I'm making call to Alfresco Webscripts which return JSON. I do this using GET
I am using Datetime.Minvalue which return 1.1.0001. This value can not be stored to
What is the best way to return only few properties to JSON Result from
I'm using MS Sql 2005. Why does this give me the correct results (returns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.