使用内容提供者,同一联系人出现多次/重复联系人-Xmarin Android

时间:2018-10-21 03:26:46

标签: xamarin.android

使用内容提供程序时,光标会多次返回多个联系人。以下是我的代码段,任何人都可以帮助我解决该问题。

       private void FillContacts()
        {

            var uri = ContactsContract.CommonDataKinds.Phone.ContentUri;
            string[] projection =
             { ContactsContract.Contacts.InterfaceConsts.Id,
                ContactsContract.Contacts.InterfaceConsts.DisplayName,
                ContactsContract.CommonDataKinds.Phone.Number };

            string selection = ContactsContract.Contacts.InterfaceConsts.Id;
            var cursor = ContentResolver.Query(uri, projection, selection, null, null);
       if (cursor.MoveToFirst()) 
       {  
            do {  
                    contactList.Add(new Contact
                   {
                       DisplayName = cursor.GetString(cursor.GetColumnIndex(projection[1])),
                     Id = cursor.GetLong(cursor.GetColumnIndex(projection[0])),

                              DisplayNumber = cursor.GetString(cursor.GetColumnIndex(projection[2]))
                 });
            } while (cursor.MoveToNext());  
        }
            cursor.Close();
            ContactsAdapter contactsAdapter = new ContactsAdapter(this, contactList);
            contactsListView.Adapter = contactsAdapter;

        }

0 个答案:

没有答案