Problem with SearchBar and a ListView

Hi, 
I have a listview with this code:

<xForms:SfListView x:Name="lstData" 
                       ItemSize="50"
                       ItemSpacing="3"
                       SelectionBackgroundColor="Transparent"
                       ItemTapped="LstData_OnItemTapped">
        <xForms:SfListView.LayoutManager>
            <xForms:GridLayout SpanCount="1" />
        </xForms:SfListView.LayoutManager>
        <xForms:SfListView.ItemTemplate>
            <DataTemplate>
                <Frame BorderColor="Chartreuse" x:Name="frame" CornerRadius="10" BackgroundColor="Black" Padding="2" >
                    <Grid >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="0.5*" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="0.5*" />
                            <ColumnDefinition Width="0.5*" />
                        </Grid.ColumnDefinitions>
                                <Label Grid.Row="0" Grid.Column="0" HorizontalOptions="Center" Text="Nombre" TextColor="Chartreuse" FontFamily="{StaticResource FuenteNormal}" />
                                <Label Grid.Row="0" Grid.Column="1" HorizontalOptions="Center" Text="Marca" TextColor="Chartreuse" FontFamily="{StaticResource FuenteNormal}" />
                                <Label Grid.Row="0" Grid.Column="2" HorizontalOptions="Center" Text="Pack" TextColor="Chartreuse" FontFamily="{StaticResource FuenteNormal}" />
                                <Label Grid.Row="0" Grid.Column="3" HorizontalOptions="Center" Text="Precio" TextColor="Chartreuse" FontFamily="{StaticResource FuenteNormal}" />
                                <Label Grid.Row="1" Grid.Column="0" HorizontalOptions="Center" Text="{Binding EmpName}" FontSize="23" TextColor="Chartreuse" FontFamily="{StaticResource FuenteNegrita}" />
                                <Label Grid.Row="1" Grid.Column="1" HorizontalOptions="Center" Text="{Binding Company}" FontSize="23" TextColor="Chartreuse" FontFamily="{StaticResource FuenteNegrita}" />
                                <Label Grid.Row="1" Grid.Column="2" HorizontalOptions="Center" Text="{Binding Qualification}" FontSize="23" TextColor="Chartreuse" FontFamily="{StaticResource FuenteNegrita}" />
                    </Grid>
                </Frame>
            </DataTemplate>
        </xForms:SfListView.ItemTemplate>
    </xForms:SfListView>


...in this page i have an icon in the toolbar that navigate to a xaml searchpage with a Search Bar with this code:

<SearchBar        x:Name="barraBusqueda"
                           TextChanged="BarraBusqueda_OnTextChanged"
                           BackgroundColor="Black"
                           TextColor="#7FFF00" 
                           FontSize="20" 
                           Placeholder="BUSCAR..."
                           Text="{Binding SearchedText, Mode=TwoWay}"
                           PlaceholderColor="Chartreuse">
</SearchBar>

How can use this SearchBar to search inside the Listview and show the EmpName corresponding with the typed text inside SearchBar??

I´m sorry, i´m a begineer and i don´t see how to achieve it.
Thanks for your help.


7 Replies

LN Lakshmi Natarajan Syncfusion Team May 26, 2020 11:01 AM UTC

Hi Luis, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “How to search ListView from another page’ from our end. We would like to inform you that you can achieve your requirement by creating another ListView with FilteredSource binded to the ItemsSource in the SearchPage. Please refer the following code snippets to achieve your requirement, 
 
Xaml: Define ListView in the SearchPage.  
<ContentPage.Content> 
    <StackLayout> 
        <SearchBar x:Name="barraBusqueda" 
                TextChanged="BarraBusqueda_TextChanged" 
                BackgroundColor="Black" 
                TextColor="#7FFF00"  
                FontSize="20"  
                Placeholder="BUSCAR..." 
                Text="{Binding SearchedText, Mode=TwoWay}" 
                PlaceholderColor="Chartreuse" 
                HeightRequest="50"> 
        </SearchBar> 
 
        <syncfusion:SfListView x:Name="listView"> 
            <syncfusion:SfListView.ItemTemplate> 
                <DataTemplate> 
                    <local:TemplateViewCell/> 
                </DataTemplate> 
            </syncfusion:SfListView.ItemTemplate> 
        </syncfusion:SfListView> 
    </StackLayout> 
</ContentPage.Content> 
 
Code behind: In the TextChanged event, set the SfListView.ItemsSource with the filtered source. 
public partial class SearchPage : ContentPage 
{ 
    SearchBar searchBar; 
    ObservableCollection<Contacts> FilteredSource { get; set; } 
 
    public SearchPage () 
    { 
        InitializeComponent (); 
    } 
 
    private void BarraBusqueda_TextChanged(object sender, TextChangedEventArgs e) 
    { 
        searchBar = (sender as SearchBar); 
        FilteredSource = new ObservableCollection<Contacts>(); 
 
        if (searchBar == null || searchBar.Text == "") 
        { 
            listView.ItemsSource = null; 
            return; 
        } 
 
        foreach (var item in viewModel.contactsinfo) 
        { 
            if (item.ContactName.ToLower().Contains(searchBar.Text.ToLower())) 
                FilteredSource.Add(item); 
        } 
        listView.ItemsSource = FilteredSource; 
    } 
} 
 
We have prepared sample and attached video for the same in the following links, 
 
Please check the sample and let us know if this helpful. If your requirement is other than this, then kindly revert us back with more details of your requirement which would be helpful for us to check on it and provide you the solution as soon as possible. 
 
Regards, 
Lakshmi Natarajan 
 



LG Luis Gomez May 26, 2020 04:15 PM UTC

Thank you for the answer
I´m not able to make it work...
Lets see if i can explain it better...

I have an SQLite model with this code:

using SQLite;
namespace AppListo
{
    public class Employee
    {
        [PrimaryKey, AutoIncrement]
        public long EmpId
        { get; set; }
        [NotNull]
        public string EmpName
        { get; set; }
        public string Company
        { get; set; }
        public string Designation
        { get; set; }
        public string Department
        { get; set; }
        public bool Favorito
        { get; set; }
        public int Precio
        { get; set; }
        public string Qualification
        { get; set; }
    }
}

and i retrieve a data list from the database with:

public List<Employee> GetAllEmployees()
        {
            return dbConn.Query<Employee>("Select * From [Employee]");
        }

...and i show the list in a content page with this xaml code:

<ListView x:Name="lstData" HasUnevenRows="false" Header="Header Value" Footer="Footer" ItemSelected="OnSelection" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Horizontal" Padding="5,5,5,5">
                                <Label Text="{Binding EmpName}" FontSize="Medium" />
                                <Label Text="{Binding Designation}" FontSize="Medium" />
                                <Label Text="{Binding Department}" FontSize="Medium" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
</ListView>

...and this code behind:

var vList = App.DAUtil.GetAllEmployees();
lstData.ItemsSource = vList;


...what i want to know is how to search inside SQLite database for EmpName using the SearchBar 

Sorry if i don´t explain very well
Thanks in advance


LN Lakshmi Natarajan Syncfusion Team May 27, 2020 11:44 AM UTC

Hi Luis, 
 
Thank you for the update. 
 
We have checked the reported query “How to filter SfListView items” from our end. We would like to inform you that you can achieve your requirement using Filter predicate of DataSource to filter the items. We have documented the same in our online document. Please refer the same from the following links, 
 
 
Please let us know if you need further assistance. 
 
Regards, 
Lakshmi Natarajan 
 



LG Luis Gomez May 30, 2020 10:52 AM UTC

Hi
I´m so sorry, but i´m not able to adapt this guides you link me to my personal project...
Could you be more precise, once you know how it works my SQLite database?
Sorry, but i´m just a newbie and it´s so difficult to me to adapt your solution to my code...
Thank you for your understanding.


CS Chandrasekar Sampathkumar Syncfusion Team June 1, 2020 12:40 PM UTC

Hi Luis, 
Thank you for the update. 
We have prepared sample based on the code snippets provided. You can achieve your requirement by using SfListView.DataSource.Filter predicate to filter the EmpName from SearchBar. Please refer the following code snippet for more reference, 
C#: Filter items in BarraBusqueda_TextChanged event based on text entered 
private void BarraBusqueda_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    lstData.DataSource.Filter = FilterContacts; 
    lstData.DataSource.RefreshFilter(); 
} 
 
private bool FilterContacts(object obj) 
{ 
    if (barraBusqueda == null || barraBusqueda.Text == null) 
        return true; 
 
    var contacts = obj as Employee; 
    if (contacts.EmpName.ToLower().Contains(barraBusqueda.Text.ToLower())) 
        return true; 
    else 
        return false; 
} 
We have attached the tested sample for your reference and you can download the same using the following link, 
Sample Link: SfListViewSample 
Video: VideoSample 
You can also refer our online documentation on Filtering in ListView for more reference using the following link, 
We observed that you have provided code snippets for Xamarin ListView instead of SfListView in the previous update. If you are using Xamarin ListView you can update ListView ItemsSource as text entered in SearchBar like in the following code snippet to achieve your requirement, 
C#: Updating ListView ItemsSource based on text entered 
List<Employee> vList = new List<Employee>(); 
public MainPage() 
{ 
    InitializeComponent(); 
 
    vList = vm.GetAllEmployees(); 
    lstData.ItemsSource = vList; 
} 
 
private void BarraBusqueda_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    lstData.ItemsSource = vList.Where(w => w.EmpName.ToLower().Contains(barraBusqueda.Text.ToLower())); 
} 
Please refer the output using Xamarin ListView using the following link, 
Video: VideoSample 
Please let us know if you need further assistance. 
Regards, 
Chandrasekar Sampathkumar 



LG Luis Gomez June 1, 2020 01:53 PM UTC

Thank you so much!
it works!
I have to tell you...it makes me happy!
Great Job
Best Regards



CS Chandrasekar Sampathkumar Syncfusion Team June 2, 2020 07:04 AM UTC

Hi Luis,  
Thanks for the update. 
We are glad that the reported issue have been resolved at your end. Please let us know if you need any further update. As always we are happy to help you out. 
Regards, 
Chandrasekar Sampathkumar 


Loader.
Up arrow icon