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