Is it possible to read more than one contact from the contact list from my app.??
what i found is how to receive a list of contacts and loop through the list.
How to read contacts on Android 2.0
but is it possible to select a few of them from the list of contacts.do i need to create a seperate layout for selecting the contacts of my choice and load that layout with data from the contacts list?? please help.
Obviously you are able to select contacts of your choice from list of contacts.But you just need to find a proper "where" clause in query(i.e. what type of contacts you want from list).
And yes,you need to create separate layout to show them up.
For Example: Getting contacts with phone number
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, ContactsContract.Contacts.HAS_PHONE_NUMBER+"='true'", null, null); // gives you the list of contacts who has phone numbers
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
Hope,i get your requirement correctly.If not,please let me know!
Basically you need to iterate through all contacts with their unique
id called contact_id.
Avoid using cursors inside cursors in the loop.
try this demo app. see how quickly it reads contacts in a proper android way
Source code attached
Related
I am developing an android application and I need to know all the information about phone contacts.
I developed a function to get the name and number of all the contacts, but I need all the information about particular contact such as email, date, favorite or not, image, social links if available.
I got id, name and number from following:
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
I used ContactsContract.Contacts to get _ID and DISPLAY_NAME, but
ContactsContract.CommenDataKinds.Phone to get the NUMBER. Is it correct?
Please explain the difference between the two methods.
Is the _ID a unique ID for all the contacts?
After a long discussion with #pskink I finally found the solution to list all related information for each contact in the directory.
First of all, create a cursor:
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
And after that, you can dumb the cursor to show all the informations and see each contact and keywords it needs to use, like (custom_ringtone, display_name, photo_uri, is_primary, ..) by using this line of code:
DatabaseUtils.dumpCursor(cursor);
Special thanks to #pskink
Is it possible to retrieve all the phone numbers with just one query(it's ok even if it's a bit slow)?
I found many topics that suggest to make query for the contact list and then a query for each contact but that's really slow.
The only solutions that I found with one query, retrieve just the main number. I need all the numbers of each contact plus the type.
Thanks.
try this
Cursor cursor = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, Phone.DISPLAY_NAME + " ASC");
I am developing an application in which i am able to read the contacts. I refereed this Link.
My code to read contact and basic information is as follow:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactID = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
}
phones.close();
Now I want to get the following things from contact as :
1.Type of contact as it is of Device, sim, google, face book etc contact.
2.The number of email for particular contact.
3.Total account(Face book, whats app, skype etc) count for the contact.
4.Total Event(Birthday, anniversary etc) count.
5.The contact is favorite or not
6.The contact is in any device group(Family, Friends) and yes then which groups?
I searched on google but not able to do the desired task.
Please suggest me what should i do.
I want to create a single Cursor that contains both given name, family name and phone nr. These columns are not located together in any of the containers available under ContactsContract and the only way I've been able to get this information is by first getting a cursor for the names and then getting phone numbers by creating a separate cursor for every contact. This solution forces me to read the data to a local data structure instead of just using an adapter on the cursor and imposes a lot of performance overhead (around ~5s with ~140 contacts with phone numbers).
Is there any way to create two cursors and then join the tables? Or is there any other way?
I have been struggling with this question for the last two days and read about everything I found on google, but really can't get anything working as I want too. This can't really be impossible, or is it?
Thanks in advance!
You can get the DISPLAY_NAME and NUMBER from ContactsContract.CommonDataKinds.Phone. Try this:
Uri uri = Phone.CONTENT_URI;
String[] projection = new String[]
{Phone.DISPLAY_NAME, Phone.NUMBER, Phone.CONTACT_ID, Phone._ID}
Cursor contactsCursor = getContentResolver().query(uri, projection, null, null, null);
i want to get contact name and phone number in a list view using cursors and simple cursor adaptor. I have seen codes which loops over all the contacts in the database. Is there an efficient way to do this by merging cursors and using simple cursor adaptor to make a list view?
You get everything about contacts in this Cursor
Cursor cursor = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
But the to get the name or id from this cursor you have to loop through there is no other possibility.