-1

I'm creating an app which require Contacts with their phone numbers. I'm able to get the Contact name list using following code

private Contact[] contact_read;
private Cursor mCursor;

String[] projection = new String[] { Contacts.HAS_PHONE_NUMBER,
        Contacts._ID, Contacts.DISPLAY_NAME };

mCursor = managedQuery(Contacts.CONTENT_URI, projection,
        Contacts.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
        Contacts.DISPLAY_NAME);

if (mCursor != null) {
    mCursor.moveToFirst();
    contact_read = new Contact[mCursor.getCount()];

int j = 0;
    do {

        contact_read[j] = new Contact(mCursor.getString(mCursor
                .getColumnIndex(Contacts.DISPLAY_NAME)));
        j++;
    } while (mCursor.moveToNext());

} else {
    System.out.println("Cursor is NULL");
}

The Contact class is

private static class Contact {
    private String name = "";

    public Contact() {
    }

    public Contact(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

But when I add the following code to get phone number, the app crashes

ContentResolver cr = getContentResolver();
String id = mCursor.getString(mCursor.getColumnIndex(Contacts._ID));
Cursor pCur = cr.query(Phone.CONTENT_URI,null,Phone.CONTACT_ID +" = ?", new String[]{id}, null);
pCur.moveToFirst();

int j = 0;
do {
       contact_read[j] = new Contact(pCur.getString(pCur.getColumnIndex(Phone.NUMBER)));
        j++;
} while (pCur.moveToNext());

Any suggestions from your side what changes need to be done to get the phone numbers??

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
Vishal Afre
  • 1,013
  • 3
  • 12
  • 39

1 Answers1

0

This is the code for getting all the contacts from our phone

while (phones.moveToNext())
        {
          String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
           arraylist_connect.add(new Connect(name,phoneNumber,"Mobile"));

        }

        phones.close();
Akhil
  • 158
  • 5