Igor Strekha
Igor Strekha

Reputation: 194

How create list of Android Contacts with xamarin?

How create list (FirstName, LastName, PhoneNumber) ?

My code created only FirstName and 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;
    }

I take for example one element of list "Kate Parry" and Split this string.

My main problem is that come 2000 email addresses. Find ' ' and filtered.

Upvotes: 0

Views: 1065

Answers (1)

Yuri S
Yuri S

Reputation: 5370

    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; }
}

Upvotes: 1

Related Questions