2014-03-12 122 views
0

我想通過Group Name獲取Android設備的本地地址簿中的所有聯繫人。如「家庭」,「朋友」,「工作」...像Android設備中的地址簿。如何在Android中按組名稱獲取聯繫人列表?

這裏是我的代碼:

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(); 
    } 

我試圖讓組通訊簿的「朋友」的所有聯繫,但它得到的所有組的所有聯繫人。

回答

0

嘗試

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); 
} 
相關問題