Search Data From Xamarin.Forms List View

Introduction

In this article, we are going to search some data from ListView of Xamarin.Forms. Firstly, we will create a list in Xamarin.Forms PCL application. Then, add some data in the list and make a search bar to search the data from the list.

What is Search Bar?

It is part of UI elements of Xamarin.Forms and is used to search the data from a list or web page or anything. You can place the search bar to make a search from web or from anywhere else. Most of us use search bar on Google homepage or on any other search engine.

Xamarin 
Implementation

We are going to code in the following steps.

  • Creating a UI
  • Make a class and set binding to List View
  • Add data to the list and add Functionality to search bar 

Creating a UI

First of all, create a Xamarin.Forms project. If you are going to create your first Xamarin.Forms application, then this article will be helpful for you. So, create an application and make a ListView and search bar on XAML file. We can use stack layout and add a search bar and list view in stack layout. Search bar is placed on top of list.

Xaml

  1. <StackLayout>  
  2.     <SearchBar></SearchBar>  
  3.     <ListView x:Name="list">  
  4.         <ListView.ItemTemplate>  
  5.             <DataTemplate>  
  6.                 <TextCell>   
  7.                 </TextCell>  
  8.             </DataTemplate>  
  9.         </ListView.ItemTemplate>  
  10.     </ListView>  
  11. </StackLayout>  

Add Binding to List View

In the above code, we create a List View and a text cell in that. Now, we are going to set binding to the ListView. So, the data is shown properly in a text cell.

Firstly, make a class of contacts that contains two properties Name and Num. Just like this.

Class Code

  1. public class Contacts  
  2.   {  
  3.       public string Name { get; set; }  
  4.       public string Num { get; set; }  
  5.       public string imgsource { get; set; }  
  6.   }  

Now, add the binding to text cell of a ListView. Replace the text cell code from your previous XAML.

  1. <TextCell Text="{Binding Name}" Detail="{Binding Num}"> </TextCell>  

Here, our class is made and binding is set. Now, it’s time to do some code in xaml.cs file.

Add data in ListView and functionality to Search Bar

First, we will create a list of our contacts class, then initialize it and add some data. After this, we will set the item source of ListView to the list of contacts. This code will add the data to our ListView.

  1. public List<Contacts> tempdata;  
  2. public MainPage()  
  3. {  
  4.     InitializeComponent();  
  5.     data();  
  6.     list.ItemsSource = tempdata;  
  7.       
  8. }  
  9.   
  10. oid data()  
  11. {  
  12.     // all the temp data  
  13.     tempdata = new List<Contacts> {  
  14.         new Contacts(){ Name = "umair", Num = "2323423"},  
  15.         new Contacts(){ Name = "saleh", Num = "23423"},  
  16.         new Contacts(){ Name = "umair", Num = "233423423"},  
  17.         new Contacts(){ Name = "sanat", Num = "2423"},  
  18.         new Contacts(){ Name = "jawad", Num = "323423"},  
  19.         new Contacts(){ Name = "shan", Num = "2323423"},  
  20.         new Contacts(){ Name = "ahmed", Num = "2323423"},  
  21.         new Contacts(){ Name = "abc", Num = "2323423"},  
  22.         new Contacts(){ Name = "umair", Num = "2323423"},  
  23.         new Contacts(){ Name = "etc", Num = "2323423"},  
  24.     };        

Output

Xamarin

Adding functionality

Make an event handler of Text Changed functionality of search bar. Just like this.

  1. <SearchBar TextChanged="SearchBar_TextChanged"></SearchBar>   

Code 

  1. private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)  
  2.       {  
  3.           //thats all you need to make a search  
  4.   
  5.           if (string.IsNullOrEmpty(e.NewTextValue))  
  6.           {  
  7.               list.ItemsSource = tempdata;  
  8.           }  
  9.   
  10.           else  
  11.           {  
  12.               list.ItemsSource = tempdata.Where(x => x.Name.StartsWith(e.NewTextValue));  
  13.           }  
  14.       }  

Explanation

Whenever the text in search bar is changed, it can work on two possible conditions. If the text is null or empty, set the item source of list to temp data. Temp data contains a list of all the data. Else it can change the item source of list according to LINQ query given. This query works on tempdata and finds if the given text in the characters match with the starting values of name.

Now, search some data and see what happens.

Output 1

Xamarin

Output 2

Xamarin 

That’s all. Our search bar is working properly.

Complete XAML and Code

Xaml 

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:crossplatformapp"  
  5.              x:Class="crossplatformapp.MainPage">  
  6.   
  7.     <StackLayout>  
  8.         <SearchBar TextChanged="SearchBar_TextChanged"></SearchBar>  
  9.         <ListView x:Name="list">  
  10.             <ListView.ItemTemplate>  
  11.                 <DataTemplate>  
  12.                     <TextCell Text="{Binding Name}" Detail="{Binding Num}">   
  13.                           
  14.                     </TextCell>  
  15.                 </DataTemplate>  
  16.             </ListView.ItemTemplate>  
  17.         </ListView>  
  18.           
  19.     </StackLayout>  
  20.     
  21. </ContentPage>   

Code 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7.   
  8. namespace crossplatformapp  
  9. {  
  10.     public partial class MainPage : ContentPage  
  11.     {  
  12.         public List<Contacts> tempdata;  
  13.         public MainPage()  
  14.         {  
  15.             InitializeComponent();  
  16.             data();  
  17.             list.ItemsSource = tempdata;  
  18.               
  19.         }  
  20.   
  21.         public void data()  
  22.         {  
  23.             // all the temp data  
  24.             tempdata = new List<Contacts> {  
  25.                 new Contacts(){ Name = "umair", Num = "2323423"},  
  26.                 new Contacts(){ Name = "saleh", Num = "23423"},  
  27.                 new Contacts(){ Name = "umair", Num = "233423423"},  
  28.                 new Contacts(){ Name = "sanat", Num = "2423"},  
  29.                 new Contacts(){ Name = "jawad", Num = "323423"},  
  30.                 new Contacts(){ Name = "shan", Num = "2323423"},  
  31.                 new Contacts(){ Name = "ahmed", Num = "2323423"},  
  32.                 new Contacts(){ Name = "abc", Num = "2323423"},  
  33.                 new Contacts(){ Name = "umair", Num = "2323423"},  
  34.                 new Contacts(){ Name = "etc", Num = "2323423"},  
  35.             };  
  36.         }  
  37.   
  38.         private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)  
  39.         {  
  40.             //thats all you need to make a search  
  41.   
  42.             if (string.IsNullOrEmpty(e.NewTextValue))  
  43.             {  
  44.                 list.ItemsSource = tempdata;  
  45.             }  
  46.   
  47.             else  
  48.             {  
  49.                 list.ItemsSource = tempdata.Where(x => x.Name.StartsWith(e.NewTextValue));  
  50.             }  
  51.         }  
  52.     }  
  53. }  

Up Next
    Ebook Download
    View all
    Learn
    View all