【问题标题】:How create list of Android Contacts with xamarin?如何使用 xamarin 创建 Android 联系人列表?
【发布时间】:2016-11-02 17:03:03
【问题描述】:

如何创建列表(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 个电子邮件地址。找到 ' ' 并过滤。

【问题讨论】:

  • 所以,让我澄清一下我是否做对了。您需要名字、姓氏和电话号码吗?从我所见,您决定使用拆分空间的人是否有名字或姓氏?如果一个人只有名字怎么办,你想过滤掉他吗?你根本就没有电话号码。对吗?

标签: xamarin xamarin.android android-contacts


【解决方案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; }
}

【讨论】:

  • 干得好!但是如果用户只使用姓氏创建电话联系人?如何以其他方式获取contact.LastName?并请统一代码(如果电话联系没有电话号码)。我想获取所有列表联系人(有电话号码和没有电话号码)。对不起我的英语+))
  • 你不觉得自己可以从这里走吗? :-)
猜你喜欢
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-01
  • 2010-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多