Skip to content Skip to sidebar Skip to footer

Get Android Contact Phone Number List

I am new to android.When i try to get contact names its working fine but i want to get numbers only, but i am not able to do so. My code is:- package com.example.sqllitecontactlis

Solution 1:

getNumber(this.getContentResolver()); 


publicvoidgetNumber(ContentResolver cr)
{
    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
            // use the cursor to access the contacts    while (phones.moveToNext())
    {
      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
             // get display name
      phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
              // get phone numberSystem.out.println(".................."+phoneNumber); 
    }

}

activity_main.xml

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><ListViewandroid:layout_width="match_parent"android:layout_height="fill_parent"android:id="@+id/lv"/></RelativeLayout>

MainActivity.java

publicclassMainActivityextendsActivity {

     String phoneNumber;
     ListView lv;
     ArrayList <String> aa= newArrayList<String>();
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
         lv= (ListView) findViewById(R.id.lv);

        getNumber(this.getContentResolver()); 
    }

    publicvoidgetNumber(ContentResolver cr)
    {
        Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
          String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          System.out.println(".................."+phoneNumber); 
          aa.add(phoneNumber);
        }
                 phones.close()// close cursorArrayAdapter<String> adapter = newArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,aa);
          lv.setAdapter(adapter);
                  //display contact numbers in the list
    }
      }

snap shot

enter image description here

Make sure you have this in manifest

<uses-permissionandroid:name="android.permission.READ_CONTACTS"/>

Solution 2:

My solution to recover all contacts:

Cursorcursor=null;
    try {
        cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
        intcontactIdIdx= cursor.getColumnIndex(Phone._ID);
        intnameIdx= cursor.getColumnIndex(Phone.DISPLAY_NAME);
        intphoneNumberIdx= cursor.getColumnIndex(Phone.NUMBER);
        intphotoIdIdx= cursor.getColumnIndex(Phone.PHOTO_ID);
        cursor.moveToFirst();
        do {
            StringidContact= cursor.getString(contactIdIdx);
            Stringname= cursor.getString(nameIdx);
            StringphoneNumber= cursor.getString(phoneNumberIdx);
            //...
        } while (cursor.moveToNext());  
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

You need this permission in your manifest :

<uses-permissionandroid:name="android.permission.READ_CONTACTS" />

I hope I have helped you!

Solution 3:

.@Arpit:

add the permission in your manifest.

BUT MOST IMPORTANTLY... Didn't you noticed that just below the line where you formatted the phoneNumber you did not add the names? You should add the name to the number just the way you added the phoneNumber (I enclosed in the code below):

..System.out.println(".................."+phoneNumber); 
aa.add(name);
aa.add(phoneNumber);

the output of the above code will be separate names and numbers on your listview(like this):


name

number

name

in order to display that in one line, you can do this:

aa.add(name+"\n"+phoneNumber);

and the output will be like this:


name number


name number


name number


good luck!

Solution 4:

Find the solution below, It will work for getting contact no from contactlist.

You need permission like:

 android:name="android.permission.READ_CONTACTS"/>

Then, Calling the Contact Picker:

Intentintent=newIntent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

Then,

@OverridepublicvoidonActivityResult(int reqCode, int resultCode, Intent data) {
 super.onActivityResult(reqCode, resultCode, data);

 switch (reqCode) {
  case (PICK_CONTACT) :
   if (resultCode == Activity.RESULT_OK) {
    UricontactData= data.getData();
    Cursorc=  managedQuery(contactData, null, null, null, null);
     if (c.moveToFirst()) {
      Stringname= c.getString(c.getColumnIndexOrThrow(People.NAME));
      // TODO Whatever you want to do with the selected contact name.
    }
  }
  break;
  }
 }

Post a Comment for "Get Android Contact Phone Number List"