简体   繁体   English

显示一个具有多个号码的联系人姓名

[英]Display one Contact name with multiple numbers

I am trying to retrieve contacts from my phone that has only numbers and put them into an arrayList, view them in lazy adapter and on click of name I would like show only numbers. 我试图从只有数字的电话中检索联系人,然后将其放入arrayList,在惰性适配器中查看它们,并在单击名称的情况下仅显示数字。 I managed to get the list of contacts and numbers but the problem is when I have a contact with multiple numbers it just adds up into the list. 我设法获得了联系人和号码的列表,但是问题是当我有多个号码的联系人时,它只是累加到列表中。

Something like for eg 像这样的东西

David +1 508 656 9043 大卫+1 508 656 9043

David +1 403 604 7053 大卫+1 403 604 7053

David +1 212 608 7053 大卫+1 212 608 7053

Instead I would like to show only David in the list and when I click it should show all the three numbers. 相反,我只想在列表中显示David,当我单击它时,应该显示所有三个数字。

I tried this: 我尝试了这个:

  void getContactNumbers()
 {
    ContentResolver cr = getContentResolver();
     String sortOrder = ContactsContract.Contacts.DISPLAY_NAME+ " COLLATE LOCALIZED ASC";
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder);
    if (cur.getCount() > 0) 
     {
        while (cur.moveToNext()) 
        {
              String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
              String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

              Log.e("contact", "...Contact Name ...." + name);
              if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
               {
                 Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                 new String[]{id}, null);
                 while (pCur.moveToNext()) 
                 {
                     String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                     Log.e("contact", "...Contact Name ...." + name + "...contact Number..." + phoneNo);
                 }

                 pCur.close();
            }
        }
    }
}

How to solve this part? 如何解决这部分?

Thanks! 谢谢!

Thanks Harshid. 谢谢哈希德。 There was selection change instead of IN_VISIBLE_GROUP + " = '1'"; 有选择更改,而不是IN_VISIBLE_GROUP +“ ='1'”; - I added HAS_PHONE_NUMBER + " = '1'"; -我添加了HAS_PHONE_NUMBER +“ ='1'”;

All contacts came up.. Hope the below code helps others!! 所有的联系人都出现了。希望下面的代码对其他人有帮助!

Thanks! 谢谢!

      final Uri uri = ContactsContract.Contacts.CONTENT_URI;
    final String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_ID
    };

    String selection  = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'";
    final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
    Cursor cur = getContentResolver().query(uri, projection, selection,  null, sortOrder);


    if (cur.getCount() > 0) 
    {
        while (cur.moveToNext()) 
        {
            String Sid = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        Log.e("contact", "...Contact Name ...." + name);
           // get the phone number
            Cursor pCur = getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                 while (pCur.moveToNext()) {
                     number = pCur.getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
         pCur.close();

    }
    }
    cur.close(); 

You have to this way query and get contact with multiple number . 您必须通过这种方式查询并与multiple number get contact

final Uri uri = ContactsContract.Contacts.CONTENT_URI;
            final String[] projection = new String[] {
                    ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.Contacts.PHOTO_URI
            };

            String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
            String[] selectionArgs = null;
            final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
            Cursor cur = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);

    if (cur.getCount() > 0) 
    {
        while (cur.moveToNext()) 
        {
              String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
              String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

              Log.e("contact", "...Contact Name ...." + name);
               // get the phone number
                Cursor pCur = getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?", new String[] { id }, null);
                     while (pCur.moveToNext()) {
                         number = pCur.getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    }
             pCur.close();

        }
    }
    cur.close();

Try this code if getting error then put comment otherwise enjoy.. 如果getting error请尝试此代码,然后添加注释,否则请享用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM