如何创建列表(FirstName,LastName,PhoneNumber)?
我的代码只创建了FirstName和LastName。
public List<PersonContact> GetPhoneContacts()
{
_phoneContacts = new List<PersonContact>();
PhoneContacts = new List<PersonContact>();
var ctx = Forms.Context;
var contactList = new List<string>();
var uri = ContactsContract.Contacts.ContentUri;
string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id, ContactsContract.Contacts.InterfaceConsts.DisplayName };
var cursor = ctx.ApplicationContext.ContentResolver.Query(uri, projection, null, null, null);
if (cursor.MoveToFirst())
{
do
{
contactList.Add(cursor.GetString(cursor.GetColumnIndex(projection[1])));
}
while (cursor.MoveToNext());
}
var sortedList = contactList.Where(s => s.Contains(" "));
foreach (var cont in sortedList)
{
string[] words = cont.Split(' ');
PersonContact contact = new PersonContact();
contact.FirstName = words[0];
contact.LastName = words[1];
_phoneContacts.Add(contact);
}
PhoneContacts = _phoneContacts;
return PhoneContacts;
}
我以列表“Kate Parry”中的一个元素和拆分此字符串为例。
我的主要问题是2000个电子邮件地址。找到''并过滤。
请帮助(
答案 0 :(得分:1)
public List<PersonContact> GetPhoneContacts()
{
var phoneContacts = new List<PersonContact>();
using (var phones = ApplicationContext.ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, null, null, null, null))
{
if (phones != null)
{
while (phones.MoveToNext())
{
try
{
string name = phones.GetString(phones.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.DisplayName));
string phoneNumber = phones.GetString(phones.GetColumnIndex(ContactsContract.CommonDataKinds.Phone.Number));
string[] words = name.Split(' ');
PersonContact contact = new PersonContact();
contact.FirstName = words[0];
if (words.Length > 1)
contact.LastName = words[1];
else
contact.LastName = ""; //no last name, is that ok?
contact.PhoneNumber = phoneNumber;
phoneContacts.Add(contact);
}
catch (Exception ex)
{
//something wrong with one contact, may be display name is completely empty, decide what to do
}
}
phones.Close(); //not really neccessary, we have "using" above
}
//else we cannot get to phones, decide what to do
}
return phoneContacts;
}
public class PersonContact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
}