Josip Šokčević

aka Šokac

How to merge contact with phone number

From Android 2.0 contact data is laid out in three tables: Contacts, Data and RawContacts. I had a problem how to link all phone numbers with their contact informations: first and last name. I haven’t found any solution for that (and that was 6 months ago). I’m not sure is this simplest solution, but it helped me.

Android Merging Contacts

Getting contacts which are in visible group and have phone number

Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1' AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER+"='1'";
String sortOrder = ContactsContract.Contacts._ID;
Cursor contactName = managedQuery(uri, projection, selection, null, sortOrder);

Getting phone numbers

// Uri uri from code above
uri=ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
projection=new String[]{
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
sortOrder = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
Cursor contactPhone = managedQuery(uri, projection, null, null, sortOrder);

What now?

Ok, now we have two cursors – for contact details and for phone numbers. I merged two cursors in ArrayList, created ArrayAdapter and set it for AutoCompleteTextView.

ArrayList

ArrayList<String> data = new ArrayList<String>();

int name = contactName.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int number = contactPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int cid=contactName.getColumnIndex(ContactsContract.Contacts._ID);
int ncid = contactPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);

if (contactName.moveToFirst() && contactPhone.moveToFirst()){
Boolean skip=false, eoc=false;
do{
skip=false;
do{
if(contactPhone.getInt(ncid)==contactName.getInt(cid)){ // same ID, merge it
data.add(contactName.getString(name)+": "+contactPhone.getString(number));
if(!contactPhone.moveToNext()){
eoc=true;
skip=true;
}

}else if(contactPhone.getInt(ncid) > contactName.getInt(cid)){
skip=true;
}else if(!contactPhone.moveToNext()){
skip=true;
eoc=true;
}
}while(!skip);
}while (contactName.moveToNext() && !eoc);
}
Collections.sort(data); // sort data :)

The code above is comparing IDs from two cursors using two while loops. If contact ID and phone ID are same, then add it in ArrayList and move contactPhone cursor to next (but not ContactName). If phone ID is greater then contact ID then move cursor contactName to next. And if contactPhone can’t move to next then leave first while loop (eoc==1).

ArrayAdapter and setAdapter

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, data);
phoneNumber.setAdapter(adapter);  // phoneNumber is AutoCompleteTextView: phoneNumber=(AutoCompleteTextView) findViewById(R.id.contactPhone);

So, that’s it. Now you have AutoCompleteTextView with contacts and their phone number(s).

Share and Enjoy:
  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • del.icio.us
  • Blogplay
  • Google Bookmarks

 

  • RSS
  • Delicious
  • Digg
  • Twitter
  • Linkedin

How to merge contact

From Android 2.0 contact data is laid out in three tables: Contacts, Data and RawContacts.  [ Read More ]

Share and Enjoy:
  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • del.icio.us
  • Blogplay
  • Google Bookmarks

Android - small snip

Android is an operating system that has the highest increase of users. Accoring to that,  [ Read More ]

Share and Enjoy:
  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • del.icio.us
  • Blogplay
  • Google Bookmarks

Novi poč... Doh, ne

Well, yes. This blog is no longer on Croatian language. There’s plenty of reason why,  [ Read More ]

Share and Enjoy:
  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • del.icio.us
  • Blogplay
  • Google Bookmarks

Tehnofobija.com - no

Nakon drugo vremena razmišljanja, kolega Vedran Vučetić i ja osnovali smo blog o tehnologiji –  [ Read More ]

Share and Enjoy:
  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • del.icio.us
  • Blogplay
  • Google Bookmarks

Zašto rootat Androi

Počela je invazija Android telefona te je broj korisnika drastično narastao. Većina ljudi su jako  [ Read More ]

Share and Enjoy:
  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • del.icio.us
  • Blogplay
  • Google Bookmarks

How to merge contact

From Android 2.0 contact data is laid out in three tables: Contacts, Data and RawContacts.  [ Read More ]

Share and Enjoy:
  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • del.icio.us
  • Blogplay
  • Google Bookmarks

Android - small snip

Android is an operating system that has the highest increase of users. Accoring to that,  [ Read More ]

Share and Enjoy:
  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • del.icio.us
  • Blogplay
  • Google Bookmarks

Twitter updates

Switch to our mobile site