【问题标题】:How to get the contact group id or name?如何获取联系人组 ID 或名称?
【发布时间】:2015-05-22 10:34:00
【问题描述】:

我无法获取存储联系人的组名。我可以得到它是否作为布尔值(IN_VISIBLE_GROUP)添加到任何组中。我不知道如何获取组名或 id。

     ContentResolver cr = this.getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
     if (cur.getCount() > 0) {
     while (cur.moveToNext()) 
            {

                 id = cur.getString(cur
                        .getColumnIndex(BaseColumns._ID));

                String name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                String group = cur
                .getString(cur
                        .getColumnIndex(ContactsContract.Contacts.IN_VISIBLE_GROUP));

我尝试过使用 ContactsContract.Groups 和 ContactsContract.Groups 和 ContactsContract.CommonDataKinds.GroupMembership 但这不是解决方案。

【问题讨论】:

  • 通常,您会使用ContactsContract.Groups-class。为什么它不适合你?
  • @Lukas 我用过 String g = cur .getString(cur1 .getColumnIndex(ContactsContract.Groups.TITLE));但返回相同的 BaseColumns._ID。请使用任何 sn-p 代码来使用 ContactsContract.Groups..
  • 您可以查看基本的Android-Contact应用程序,其源代码可以找到here
  • @Lukas 谢谢,但我得到的组名仍然出错

标签: java android


【解决方案1】:

这是如何通过title 获取组ID 的完整版本。基本上它会迭代all groups 并比较标题以找到它的id

private String getGroupId(String groupTitle) {
   Cursor cursor =getContentResolver().query(ContactsContract.Groups.CONTENT_URI,new String[]{ContactsContract.Groups._ID,ContactsContract.Groups.TITLE}, null, null, null);       
    cursor.moveToFirst();
    int len = cursor.getCount();

    String groupId = null;
    for (int i = 0; i < len; i++) {
        String id = cursor.getString(cursor.getColumnIndex(Groups._ID));
        String title = cursor.getString(cursor.getColumnIndex(Groups.TITLE));

        if (title.equals(groupTitle)) {
            groupId = id;
            break;
        }
        cursor.moveToNext();
    }
    cursor.close();

    return groupId;
}

【讨论】:

    【解决方案2】:

    请更改

     ContentResolver cr = this.getContentResolver();
    

     Uri groupURI = ContactsContract.Data.CONTENT_URI;
    
    
    Cursor cur = cr.query(groupURI , null, null, null, null);
    
    
    // Now you can get group ID from cursor
    
    String groupRowId =  cur.getString   (cur.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID));
    

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      • 2015-07-02
      • 1970-01-01
      相关资源
      最近更新 更多