We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Search ListView with Contacts

Hi,

I am listing Contact in my SfListView using:


async void PopulateContacts()

{
        var contacts = await Microsoft.Maui.ApplicationModel.Communication.Contacts.GetAllAsync();
        ListViewContacts.ItemsSource = contacts;
}


Now How can I search the items in the SfListView? I tried below but did not work:


private void OnFilterTextChanged(object sender, TextChangedEventArgs e)

{
    searchBar = (sender as Entry);



    if (ListViewProverbs.DataSource != null)
    {
        ListViewProverbs.DataSource.Filter = FilterService;
        ListViewProverbs.DataSource.RefreshFilter();
    }
}


then:


private bool FilterService(object obj)


{
    if (string.IsNullOrWhiteSpace(searchBar.Text))
    {
        ImageClearSearch.IsVisible = false;


        return true;
    }


    var contact = obj as Contacts;


    try
    {
        if (contact.DisplayName.ToLower().Contains(searchBar.Text.ToLower()))
        {
            ImageClearSearch.IsVisible = true;


            return true;
        }
        else return false;
    }
    catch
    {
        Console.WriteLine("");
    }


    return false;
}

Thanks,

Jassim


3 Replies

SV Suja Venkatesan Syncfusion Team February 6, 2023 01:30 PM UTC

Hi Jassim,


We'd like to inform you that we're encountering an "Android.Database.StaleDataException" while deploying the ItemsSource of SfListView from Microsoft. Maui. ApplicationModel.Communication.Contact. We believe the reported issue is caused by the sourcetype of DataSource not being properly updated when setting the source from a contact. The reported scenario can be resolved by scenario setting the sourcetype in the sample level to the listview datasource, as shown in the code snippets below.


Code Snippets:

var contacts = await Microsoft.Maui.ApplicationModel.Communication.Contacts.Default.GetAllAsync();

            ListViewContacts.ItemsSource = contacts;

            ListViewContacts.DataSource.SourceType = typeof(Microsoft.Maui.ApplicationModel.Communication.Contact);


Please let us know if you need any further assistance.


Regards,

Suja



JR Jassim Rahma replied to Suja Venkatesan February 6, 2023 02:19 PM UTC

Thanks. That solves the problem. But I have one more question please..

Using TextChangedEventArgs slows the typing effect because it searches for every characters in the contact booked so if you type Suja it will take time after every type of the characters.


Any suggest on how to solve this?




SV Suja Venkatesan Syncfusion Team February 7, 2023 02:19 PM UTC

Jassim, You can resolve the reported scenario by using a timer to enter full text rather than checking letter by letter in textchangedevent, as shown in the code snippet below.


private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)

        {

            //TimeSpan can be modified as per your requirement

            Device.StartTimer(TimeSpan.FromSeconds(4),()=>

            {

                FilterPredicate();

                return false;

            }

            );

        }

       public void FilterPredicate()

        {

            if (ListView.DataSource != null)

            {

                ListView.DataSource.Filter = FilterContacts;

                ListView.DataSource.RefreshFilter();

            }

            ListView.RefreshView();

        }


Loader.
Live Chat Icon For mobile
Up arrow icon