Android examples for Account:Contact
fill Maps with Contact information
//package com.java2s; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.content.Context; import android.database.Cursor; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; public class Main { public static List<HashMap<String, String>> fillMaps(Context context) { List<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>(); Cursor cur = null;/* www .j av a2 s .com*/ try { // Query using ContentResolver.query or Activity.managedQuery cur = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null); // cur = getContentResolver().query( // ContactsContract.Contacts.CONTENT_URI, null, null, null, // null); if (cur.moveToFirst()) { int idColumn = cur .getColumnIndex(ContactsContract.Contacts._ID); int displayNameColumn = cur .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); // Iterate all users do { String contactId; String displayName; String phoneNumber = ""; // Get the field values contactId = cur.getString(idColumn); displayName = cur.getString(displayNameColumn); // Get number of user's phoneNumbers int numberCount = cur .getInt(cur .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if (numberCount > 0) { Cursor phones = context .getContentResolver() .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId /* * + " and " + * ContactsContract.CommonDataKinds * .Phone.TYPE + "=" + * ContactsContract.CommonDataKinds * .Phone.TYPE_MOBILE */, null, null); if (phones.moveToFirst()) { int numberColumn = phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); // Iterate all numbers do { phoneNumber += phones .getString(numberColumn) + ","; } while (phones.moveToNext()); } } // Add values to items HashMap<String, String> i = new HashMap<String, String>(); i.put("name", displayName); i.put("key", phoneNumber); items.add(i); } while (cur.moveToNext()); } else { HashMap<String, String> i = new HashMap<String, String>(); i.put("name", "Your Phone"); i.put("key", "Have No Contacts."); items.add(i); } } finally { if (cur != null) cur.close(); } return items; } }