简体   繁体   中英

Xamarin Forms list contacts and phone numbers

I'm creating an application that should list contacts and contacts phones. The contact list I can do, but I am being unable to list the phone numbers associated with a particular contact. I have been leaning across the network to find a workable solution to my problem but I am being unable to find the solution.

This is the code that reads and lists the contacts.

public List<Contato> ListaContato()
    {
        var uri = ContactsContract.Contacts.ContentUri;

        string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id, ContactsContract.Contacts.InterfaceConsts.DisplayName, ContactsContract.Contacts.InterfaceConsts.PhotoId };

        var cursor = Context.ContentResolver.Query(uri, projection, null, null, null);

        var contactList = new List<Contato>();

        if (cursor.MoveToFirst())
        {
            do
            {
                Contato contato = new Contato();
                Telefone telefone = new Telefone();

                contato.Id = cursor.GetInt(cursor.GetColumnIndex(projection[0]));
                contato.NomeContato = cursor.GetString(cursor.GetColumnIndex(projection[1]));
                contato.FotoContato = cursor.GetString(cursor.GetColumnIndex(projection[2]));

                contato.Telefone = CarregarTelefone(contato.Id.ToString());

                contactList.Add(contato);
            }
            while (cursor.MoveToNext());
        }

        return contactList;
    }

This other method is to make the listing of phone numbers associated with a contact.

public List<Telefone> CarregarTelefone(string contactId)
    {
        //ContactsContract.CommonDataKinds.Phone.SearchDisplayNameKey
        var cursor = Context.ContentResolver.Query(Phone.ContentUri, new String[] { Phone.Number }, ContactsContract.CommonDataKinds.Identity.IdentityColumnId + "=" + contactId, null, null);
        //var c = Context.ContentResolver.Query(Phone.ContentUri, new String[] { Phone.Number }, Phone.ContentItemType + "=" + Phone.TYPE_MOBILE + " and " + Phone.CONTACT_ID + "=" + contactId, null, null);


        List<Telefone> litel = new List<Telefone>();

        //if (cursor.MoveToNext())
        //{
        //    Telefone tele = new Telefone();
        //    tele.NumeroTelefone = cursor.GetString(0);
        //    litel.Add(tele);
        //}

        while (cursor.MoveToNext())
        {
            Telefone tele = new Telefone();
            tele.NumeroTelefone = cursor.GetString(0);
            litel.Add(tele);
        }

        return litel;
    }

I have used this code as a reference, but it does not serve me fully.

cursor = contentResolver.query(Phone.CONTENT_URI, new String[]{Phone.NUMBER}, Phone.TYPE + "=" + Phone.TYPE_MOBILE+" and "+Phone.CONTACT_ID + "=" + contactId, null, null);

The code is contained in this link: ContentResolver.Query()

How do I, then, be able to list all the phone numbers of a contact in my xamarin application?


Good night people! Regarding the issue of discovering the phone's label (mobile, home, work etc) I want to say that I have already found the solution and I am sharing here with you.

public List<Telefone> CarregarTelefone(string contactId)
    {
        var uri = ContactsContract.Contacts.ContentUri;

        string[] projection = { Phone.Number, CommonColumns.Type };

        var cursor = Context.ContentResolver.Query(Phone.ContentUri, projection, ContactsContract.RawContactsColumns.ContactId + "=" + contactId, null, null);

        List<Telefone> litel = new List<Telefone>();

        if (cursor != null)
        {
            while (cursor.MoveToNext())
            {
                Telefone tele = new Telefone();
                tele.NumeroTelefone = cursor.GetString(0);
                tele.Etiqueta = CarregarEtiqueta(int.Parse(cursor.GetString(1)));
                litel.Add(tele);
            }
        }

        cursor.Close();

        return litel;
    }

Now I still need help solving the issue of listing a contact's groups (Family, Work, Schoolmates, Teachers etc). I have looked at some things on the web, but I have not yet succeeded. Any tip is very welcome.

I'm creating an application that should list contacts and contacts phones. The contact list I can do, but I am being unable to list the phone numbers associated with a particular contact. I have been leaning across the network to find a workable solution to my problem but I am being unable to find the solution.

You can't retrieve the phone numbers according to contactId because you are using the wrong filter string:

ContactsContract.CommonDataKinds.Identity.IdentityColumnId + "=" + contactId

The correct filter looks like this:

ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId + "=" + contactId

So CarregarTelefone should looks like this:

public List<Telefone> CarregarTelefone(string contactId)
{
    var cursor = this.ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, new string[] { Phone.Number }, ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId + "=" + contactId, null, null);

    List<Telefone> litel = new List<Telefone>();

    while (cursor.MoveToNext())
    {
        Telefone tele = new Telefone();
        tele.NumeroTelefone = cursor.GetString(0);
        litel.Add(tele);
    }

    return litel;
}

Update:

For search of etiquette, you can add a ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type column to the search columns:

var cursor = this.ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, new string[] { Phone.Number, Phone.InterfaceConsts.Type }, ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId + "=" + contactId, null, null);

It will give you an int value.You can compare the int with the type table to get the current type of phone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM