0

I want to get all contact in local address book of Android device by Group Name. Such as "Family", "Friends", "Work" ... like Address book in android device.

Here is my code:

 public void getAllContactsByGroup(ContentResolver cr) {

        String a  = "Friends";  
        Uri CONTENT_URI_GR = ContactsContract.Groups.CONTENT_URI;
        String GR_ID = ContactsContract.Groups._ID;
        String GR_NAME = ContactsContract.Groups.TITLE;

        Cursor cursor = getContentResolver().query(CONTENT_URI_GR, 
                null, GR_NAME + "=?", new String[] { a }, null);

        while (cursor.moveToNext())
        {
            Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
            String _ID = ContactsContract.Contacts._ID;
            String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;

            Cursor phones = cr.query(CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

            countContact = phones.getCount();
            while (phones.moveToNext())
            {
                contact_id = phones.getString(phones.getColumnIndex(_ID));
                String name = phones.getString(phones.getColumnIndex(DISPLAY_NAME));

                name1.add(name);
                _idd.add(contact_id);
            }

            phones.close();
        }
        cursor.close();
     }

I tried to get all contact in group "Friends" of Address book, but it gets all contacts of all groups.

ekad
  • 14,436
  • 26
  • 44
  • 46
binhnt
  • 97
  • 3
  • 11

1 Answers1

0

try

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Cursor cur = getContacts();

    ListView lv = getListView();

   String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};

   SimpleCursorAdapter adapter = 
            new SimpleCursorAdapter(this, 
                                    R.layout.main,
                                    cur,
                                    fields,
                                    new int[] {R.id.txtbox});
      lv.setAdapter(adapter);         
}    

private Cursor getContacts() {  
    // Run query     
    Uri uri = ContactsContract.Contacts.CONTENT_URI;

    String[] projection = 
            new String[]{ ContactsContract.Contacts._ID,
                          ContactsContract.Contacts.DISPLAY_NAME }; 
        String selection = null;
        String[] selectionArgs = null;  
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + 
            " COLLATE LOCALIZED ASC";  
        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
Biri
  • 48
  • 7