devhet2003
devhet2003

Reputation: 1

how to get note value from contact book in android

I want to have my application which gets values from contact book from Android Phone.

I have tried People, ContactContracts.Data, ContactContracts.Contact.Data and ContactsContract.CommonDataKinds to read value of NOTE value from Phone book. But Can't get successed .Please help me.

Upvotes: 0

Views: 1549

Answers (1)

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Use below Code for Get Note Value from Contacts.

Cursor cursor = getContentResolver().query(
        ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
cursor.moveToFirst();

for (int i=0; i<cursor.getCount(); i++){
    System.out.println("Hello");
    String contactId = cursor.getString(
            cursor.getColumnIndex(ContactsContract.Contacts._ID));

    String note = null;
    String[] columns = new String[] { ContactsContract.CommonDataKinds.Note.NOTE };
    String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
            ContactsContract.Data.MIMETYPE + " = ?";
    String[] whereParameters = new String[] {
            contactId, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE };
    Cursor contacts = getContentResolver().query(
            ContactsContract.Data.CONTENT_URI, columns, where,
            whereParameters, null);

    if (contacts.moveToFirst()) {
        String rv = contacts.getString(0);
        note = rv;
    } else{
        String rv = contacts.getString(i);
        note = rv;
    }
    contacts.close();
    System.out.println("Note is: " + note);

    cursor.moveToNext();            
}

Upvotes: 1

Related Questions