PocketMagic

PocketMagic

Where Technology meets magic


Android
47 Posts
BlackBerry
4 Posts
Electronics
69 Posts
Hardware
123 Posts
High Voltage
49 Posts
Image processing
2 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
76 Posts
Symbian
2 Posts
Tubes
20 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


Building a robot – Part 2 | 8885 Views | Rate 67.31
Building a robot – Part 2
uRADMonitor - Online Radiation monitoring station | 16132 Views | Rate 66.94
uRADMonitor - Online Radiation monitoring station
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 22132 Views | Rate 65.48
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 5839 Views | Rate 51.22
NMEA GPS Library for AVR Microcontrollers
Programmatically Injecting Events on Android - Part 2 | 6571 Views | Rate 46.6
Programmatically Injecting Events on Android - Part 2
Simple Switched power Supplies | 19135 Views | Rate 46.22
Simple Switched power Supplies
Capacitor Discharge Microspot Welder / Cutter | 13227 Views | Rate 38.9
Capacitor Discharge Microspot Welder / Cutter
A 3D Carousel View for Android | 232 Views | Rate 38.67
A 3D Carousel View for Android

 
  

Android Contacts – Searching for contacts


By Radu Motisan Posted on March 19th, 2011 , 1950 Views (Rate 2.37)



  

Once a contact is added, or even if you're only needing to access some contacts on the phone, it is important to know ways of searching for your data.
Android uses SQL to store the contacts, and the API used to manage this data resembles the SQL syntax a lot, making thing not only easier, but also offering a lot of control over your desired output.
Here's a sample to show a basic contacts search, first using a string to identify the name we need to locate, and a more general method where we define a projection (determining what columns to be read), additional parameters like visible/invisible contacts and sorting:

To locate a particular contact we construct the search code like this:

  1.  
  2. String contname = "Name to Search for!";
  3. Uri lkup = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, contname);
  4. Cursor idCursor = getContentResolver().query(lkup, null, null, null, null);
  5. while (idCursor.moveToNext()) {
  6. String id = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts._ID));
  7. String key = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
  8. String name = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
  9. Log.d(LOG_TAG, "search: "+id + " key: "+key + " name: "+name);
  10. }
  11. idCursor.close();
  12.  

This code will return only a few columns corresponding to ContactsContract.Contacts structure. If we need to read more details like Phone or Email, the code must be modified like this:

  1.  
  2. String contname = "Name to Search for!";
  3. Uri lkup = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, contname);
  4. Cursor idCursor = getContentResolver().query(lkup, null, null, null, null);
  5. while (idCursor.moveToNext()) {
  6. String id = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts._ID));
  7. String key = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
  8. String name = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
  9. Log.d(LOG_TAG, "search: "+id + " key: "+key + " name: "+name);
  10. // NAME: FIRST AND LAST
  11. String nameWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
  12. String[] nameWhereParams = new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
  13. Cursor nameCur = cr.query(ContactsContract.Data.CONTENT_URI, null, nameWhere, nameWhereParams, null);
  14. boolean valid = false;
  15. while (nameCur.moveToNext()) {
  16. firstName = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
  17. lastName = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
  18. String idName = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName._ID));
  19. String contactidName = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID));
  20. Log.e(LOG_TAG,"(id:"+id+") (fn:"+firstName+")(ln:"+lastName+")(rid:"+idName+")(cid:"+contactidName+")");
  21.  
  22. if (firstName!=null && lastName!=null && (firstName.length()>0 || lastName.length()>0)) {
  23. valid = true;
  24. break;
  25. }
  26. }
  27. nameCur.close();
  28. // ORGANISATIONs
  29. String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
  30. String[] orgWhereParams = new String[]{id, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
  31. Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI, null, orgWhere, orgWhereParams, null);
  32. //if (orgCur.moveToFirst()) {
  33. valid = false;
  34. while (orgCur.moveToNext()) {
  35. Company = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
  36. String idC = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization._ID));
  37. String contactidC = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.CONTACT_ID));
  38. //String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
  39. Log.e(LOG_TAG,"(id:"+id+") (c:"+Company+")(rid:"+idC+")(cid:"+contactidC+")");
  40. if (Company!=null && Company.length()>0) {
  41. valid = true;
  42. break;
  43. }
  44. }
  45. orgCur.close();
  46. }
  47. idCursor.close();
  48.  

So first we do a search to identify the CONTACT ID for a given name , then we use the ID to run another search and identify the other contact fields we're interested in . In the same way we can retriebe Phone, FAX, Email, WEB, notes, or any custom columns.

Here's a sample project:
AndroidContacts4


  

More on PocketMagic:

Atmega8 and enc28J60 for ethernet support | 8855 Views | Rate 27.33
Atmega8 and enc28J60 for ethernet support
How to make metallic sodium | 1436 Views | Rate 26.59
How to make metallic sodium
Tube: USN-5J29 | 1089 Views | Rate 25.93
Tube: USN-5J29
How to set the AVR Fusebits | 2551 Views | Rate 25.26
How to set the AVR Fusebits
Dual H-Bridge for controlling two motors | 2107 Views | Rate 24.79
Dual H-Bridge for controlling two motors
Tube: GP-5 (ГП-5) | 637 Views | Rate 24.5
Tube: GP-5 (ГП-5)

5 Responses to “Android Contacts – Searching for contacts”

  1. 1
    Ayaz Alavi:

    Hi,
    I need to search for contacts by _ID parameter. I have got id and I wanna query ContactContract API for the contact information. Any help will be appreciated.
    Thanks
    Ayaz Alavi

  2. 2
    Mare:

    Hi Ayaz. Use ContentUris.withAppendedId() or Uri.withAppendedPath() (see here for example: http://developer.android.com/guide/topics/providers/content-providers.html#querying)

    My question: how do I make some more complex queries than just “select…from…where”? I want to get all contacts that have more than one phone (see my question on stackoverflow: http://stackoverflow.com/questions/6404689/all-contacts-that-have-more-than-one-phone-number). Is there a way to get them using only one query?

  3. 3
    Mare:

    Damn url parser… the links should have been: http://developer.android.com/guide/topics/providers/content-providers.html#querying and http://stackoverflow.com/questions/6404689/all-contacts-that-have-more-than-one-phone-number

  4. 4
    sabrin:

    I have used how declare method move ToFirst in a class.I’am Starter help me.thx:)

  5. 5
    Truiton:

    Thanks Dude, used your first name last name code.

Thank you for commenting. Your comment won't show until approved, which can take some time.

Please copy the string QUfBpQ to the field below: