locked
How to get the contacts first name,middle name,last name instead of having display name in xmarine? RRS feed

  • Question

  • User369992 posted

    Hi, I am working in c# xmarine android application, I am able to get phone contacts display name but I am not able to get contacts first name,middle name,last name instead of having display name? how to resolve?

    Tuesday, February 25, 2020 12:03 PM

Answers

  • User369978 posted

    Have you enabled permission in Manifest ? Check my sample below.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, February 26, 2020 7:54 AM

All replies

  • User369978 posted

    The code to get contact information

         ICursor cursor = ContentResolver.Query(ContactsContract.Data.ContentUri, null, null, null);
    
            int indexGivenName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GivenName);
            int indexFamilyName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FamilyName);
            int indexDisplayName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName);
    
    
            while (cursor.MoveToNext())
            {
                String given = cursor.GetString(indexGivenName);
                String family = cursor.GetString(indexFamilyName);
                String display = cursor.GetString(indexDisplayName);
            }
    

    The code above worked ever ,but now we can only get DisplayName , i think the api is deprecated and broken though i didn't find the related documentation .

    Workaround

    DisplayName(full name) is consists of first name and last name , and contains an space character between them , so split the full name by space character , we can get first name and last name .

        String display = cursor.GetString(indexDisplayName);
        string firstName = display.Split(" ", StringSplitOptions.None)[0];
                string lastName = display.Split(" ", StringSplitOptions.None)[1];
    

    PS : This workaround only applys for the firstName(lastName ) without space character .

    Wednesday, February 26, 2020 3:19 AM
  • User369992 posted

    Hi @ColeX I tried with the above code, but i am facing exception "Java.Lang.NoSuchMethodError:" now how to resolve?

    Wednesday, February 26, 2020 7:43 AM
  • User369978 posted

    Have you enabled permission in Manifest ? Check my sample below.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, February 26, 2020 7:54 AM
  • User369992 posted

    Hi @ColeX
    got your code spinet but I am facing the same problem. I am attaching the code here.Also I added permission in Manifest file. I am working in a environment like visual studio 2015.

    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_CONTACTS" />

    using System;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android;
    using Android.Content.PM;
    using Android.Provider;
    using Android.Database;
    
    
    protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.Contact);  
    
                ICursor cursor = ContentResolver.Query(ContactsContract.Data.ContentUri, null, null, null); // this line is giving error
    
                int indexGivenName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GivenName);
                int indexFamilyName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FamilyName);
                int indexDisplayName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName);
    
    
                while (cursor.MoveToNext())
                {
                    String given = cursor.GetString(indexGivenName);
                    String family = cursor.GetString(indexFamilyName);
                    String display = cursor.GetString(indexDisplayName);
                }
    
            }
    
    Wednesday, February 26, 2020 8:50 AM
  • User369978 posted

    sorry , i forgot we have to check Permission at runtime in android 6.0 , take a look at https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows#runtime-permission-checks-in-android-60 .

    Add the following code into your project .

    protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);
    
            Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);
    
            FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
            fab.Click += FabOnClick;
    
    
            if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadContacts) == (int)Permission.Granted)
            {
    
            }
            else
            {
                if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.ReadContacts))
                {
    
                    var requiredPermissions = new String[] { Manifest.Permission.ReadContacts };
    
                }
                else
                {
                    ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadContacts }, 1);
                }
            }
        }
    
    Wednesday, February 26, 2020 9:05 AM
  • User369992 posted

    hi @ColeX Thanks for the support,but same issue exist, Code is as follows.

    using System;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android;
    using Android.Content.PM;
    using Android.Provider;
    using Android.Database;
    using Android.Support.V4.Content;
    using Android.Support.V4.App;
    
    namespace RunTimePerm
    {
        [Activity(Label = "ContactActivity", MainLauncher = true)]
        public class ContactActivity : ListActivity
        {
    
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);            
    
                SetContentView(Resource.Layout.Main);       
    
                if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadContacts) == (int)Permission.Granted)
                {
                }
                else
                {
                    if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.ReadContacts))
                    {
                        var requiredPermissions = new String[] { Manifest.Permission.ReadContacts };
                    }
                    else
                    {
                        ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadContacts }, 1);
                    }
                }
    
                ICursor cursor = ContentResolver.Query(ContactsContract.Data.ContentUri, null, null, null);
    
                int indexGivenName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GivenName);
                int indexFamilyName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FamilyName);
                int indexDisplayName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName);
    
                while (cursor.MoveToNext())
                {
                    String given = cursor.GetString(indexGivenName);
                    String family = cursor.GetString(indexFamilyName);
                    String display = cursor.GetString(indexDisplayName);
                }
    
            }
    
        }
    }
    
    Wednesday, February 26, 2020 9:33 AM
  • User369978 posted

    Move the logic of Read contact into FabOnClick method .

    Uninstall the application on your android simulator .

    Wednesday, February 26, 2020 9:41 AM
  • User397241 posted

    @ColeX said:

    The code to get contact information

       ICursor cursor = ContentResolver.Query(ContactsContract.Data.ContentUri, null, null, null);
    
            int indexGivenName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GivenName);
            int indexFamilyName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FamilyName);
            int indexDisplayName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName);
    
    
            while (cursor.MoveToNext())
            {
                String given = cursor.GetString(indexGivenName);
                String family = cursor.GetString(indexFamilyName);
                String display = cursor.GetString(indexDisplayName);
            }
    

    @ColeX : Hi, I saw your note that this API might have been deprecated. Can you guide about any other way to get first name, middle name, last name, suffix from contacts?

    Monday, October 12, 2020 4:17 AM