2010-12-02 126 views

回答

9
final String[] GROUP_PROJECTION = new String[] { 
      ContactsContract.Groups._ID, ContactsContract.Groups.TITLE }; 
    cursor = getContentResolver().query(
    ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null, 
      null, ContactsContract.Groups.TITLE); 

      GlobalConfig.groupList.clear(); 
    Group g = new Group(); 
    g.GroupIdList += "0"; 
    g.setGroupTitle("ALL"); 
    GlobalConfig.groupList.add(g); 
    while (cursor.moveToNext()) { 

     String id = cursor.getString(cursor 
       .getColumnIndex(ContactsContract.Groups._ID)); 

     String gTitle = (cursor.getString(cursor 
       .getColumnIndex(ContactsContract.Groups.TITLE))); 

     if (gTitle.contains("Group:")) { 
      gTitle = gTitle.substring(gTitle.indexOf("Group:") + 6).trim(); 

     } 
     if (gTitle.contains("Favorite_")) { 
      gTitle = "Favorites"; 
     } 
     if (gTitle.contains("Starred in Android") 
       || gTitle.contains("My Contacts")) { 
      continue; 
     } 

     Group gObj = new Group(); 

     int pos = GlobalConfig.GroupContainsTitle(gTitle); 
     if (pos != -1) { 
      gObj = GlobalConfig.groupList.get(pos); 
      gObj.GroupIdList += "," + id; 
      GlobalConfig.groupList.set(pos, gObj); 

     } else { 
      gObj.GroupIdList += id; 
      gObj.setGroupTitle(gTitle); 
      GlobalConfig.groupList.add(gObj); 

     } 

     // Log.d("GrpId Title", gObj.getGroupIdList() + 
     // gObj.getGroupTitle()); 
    } 
+0

這是我的問題的完美解決方案。但是,在這裏我不明白使用GlobalConfig類和它存儲給定GroupContainsTitle的方式。所以@Abhishek可以澄清更多關於GlobalConfig類的內容。這將節省我的時間。 – Prativa 2012-06-13 05:18:31

+0

@prativa GlobalConfig只是一個簡單的靜態類,在這裏用來存儲可在整個應用程序中訪問的靜態集合。 – Abhi 2012-08-18 06:03:23

5

通過@Abhi答案是確定的,但有一些限制:

  • 將列表中刪除聯繫人
  • 將列出隱藏組
  • 將列出「鬼」羣體(即基團應該已經被刪除但仍然處於困境)

-

private class GroupInfo { 
    String id; 
    String title; 

    @Override 
    public String toString() { 
     return title+ " ("+id+")"; 
    } 

    public String getId() { 
     return id; 
    } 
} 

List<GroupInfo> groups = new ArrayList<GroupInfo>(); 

public void loadGroups() { 
    final String[] GROUP_PROJECTION = new String[] { 
      ContactsContract.Groups._ID, 
      ContactsContract.Groups.TITLE, 
      ContactsContract.Groups.SUMMARY_WITH_PHONES 
      }; 

    Cursor c = getContentResolver().query(
      ContactsContract.Groups.CONTENT_SUMMARY_URI, 
      GROUP_PROJECTION, 
      ContactsContract.Groups.DELETED+"!='1' AND "+ 
      ContactsContract.Groups.GROUP_VISIBLE+"!='0' " 
      , 
      null, 
      null); 
    final int IDX_ID = c.getColumnIndex(ContactsContract.Groups._ID); 
    final int IDX_TITLE = c.getColumnIndex(ContactsContract.Groups.TITLE); 

    Map<String,GroupInfo> m = new HashMap<String, GroupInfo>(); 

    while (c.moveToNext()) { 
     GroupInfo g = new GroupInfo(); 
     g.id = c.getString(IDX_ID); 
     g.title = c.getString(IDX_TITLE); 
     int users = c.getInt(c.getColumnIndex(ContactsContract.Groups.SUMMARY_WITH_PHONES)); 
     if (users>0) { 
      // group with duplicate name? 
      GroupInfo g2 = m.get(g.title); 
      if (g2==null) { 
       m.put(g.title, g); 
       groups.add(g); 
      } else { 
       g2.id+=","+g.id; 
      } 
     } 
    } 
    c.close(); 
    } 
1

不需要舊的過度回答。這裏更簡單的解決方案。

final String[] GROUP_PROJECTION = new String[] { 
      ContactsContract.Groups._ID, ContactsContract.Groups.TITLE }; 
Cursor gC = getContentResolver().query(
      ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION,null,null,null); 
gC.moveToFirst(); 
while (!gC.isAfterLast()) { 
     int idcolumn = gC.getColumnIndex(ContactsContract.Groups.TITLE); 
     String Id = gC.getString(idcolumn); 
     ArrayL.add(Id); 
     gC.moveToNext(); 
} 
     LinkedHashSet<String> s = new LinkedHashSet<String>(); 
     s.addAll(ArrayL); 
     ArrayL.clear(); 
     ArrayL.addAll(s);