Neha
Neha

Reputation: 187

Accessing the contact Content Provider

I am just tring to access the contact Content Provider. for that i wrote following code

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

    startManagingCursor(cur);

    String[] result=new String[cur.getCount()];

    if(cur.moveToFirst())
    {
        int nameidx=cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
        int Ididx=cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
        String strName=cur.getString(nameidx);
        String strId=cur.getString(Ididx);

        result[cur.getPosition()]=strName+"("+strId+")";
    }while(cur.moveToNext());

   stopManagingCursor(cur);
}

but its showing only one contact name i want to show all contact available please tell me how do that.

Upvotes: 0

Views: 2764

Answers (1)

ingsaurabh
ingsaurabh

Reputation: 15269

try this instead

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

startManagingCursor(cur);

String[] result=new String[cur.getCount()];

for (boolean hasData = cursor.moveToFirst(); hasData; hasData = cursor.moveToNext())
{
    int nameidx=cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
    int Ididx=cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
    String strName=cur.getString(nameidx);
    String strId=cur.getString(Ididx);

    result[cur.getPosition()]=strName+"("+strId+")";
}
stopManagingCursor(cur);
}

Upvotes: 1

Related Questions