How To Get Android Contact List Data On My Seperate Listview In Android 2.1?
i have created one listview.now i want to show data(contact no.,name) from contactlist of android(Phonebook) on my listview. i got it for android 1.5 but i want to do it for androi
Solution 1:
If you have achieved it in 1.5 then there can be a small difference of URI. You should go for updated/changed URI of Contacts in 2.1. For further understanding here is a sample code for 2.1updated:
ContentResolvercr= getContentResolver();
Cursorcur= cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cur.moveToNext())
{
Stringid= cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
//---------------------------------------- Contact Names //------------------------------------------------------StringDisplayName= cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursornames= cr.query(ContactsContract.Data.CONTENT_URI, null,ContactsContract.Data.CONTACT_ID + " = " + id, null, null);
names.moveToNext();
StringGivenName= names.getString(names.getColumnIndex(ContactsContract.Data.DATA2));
StringFamilyName= names.getString(names.getColumnIndex(ContactsContract.Data.DATA3));
StringMiddleName= names.getString(names.getColumnIndex(ContactsContract.Data.DATA5));
names.close();
//----------------------------------------- Phone Numbers//-------------------------------------------------------StringhasPhoneNumber= cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhoneNumber.equals("1"))
{
Cursorphones= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
while (phones.moveToNext())
{
......
}
}
} // AND SO ON.... Get which ever data you need.
so above is just a sample code that I used for my task. You can get what you need by making changes accordingly. I hope it helps.
Solution 2:
This will help you getting contacts.
CursorcontactList= getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null,null, null);
For getting phone number of a particular contact,you need to use:
String id = contactList.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phoneCursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
newString[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
newString[]{id}, null);
Post a Comment for "How To Get Android Contact List Data On My Seperate Listview In Android 2.1?"