Home > Enterprise >  search is not working in ios xamarin.forms
search is not working in ios xamarin.forms

Time:06-02

i have a searchbar in my app. the code for searching is as follows:

 private void search_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (list_total_reg_std.Count != 0)
            {
                if (string.IsNullOrEmpty(e.NewTextValue))
                {
                    if (filtered == false)
                        registeredstdslist.ItemsSource = list_total_reg_std;
                    else
                        registeredstdslist.ItemsSource = list_filtered_reg_std;
                }

                else
                {
                    if (filtered == false)
                        registeredstdslist.ItemsSource = list_total_reg_std.Where(x => x.name.ToLower().Contains(e.NewTextValue) || x.stdid.ToLower().Contains(e.NewTextValue) || x.edlevel.ToString().ToLower().Contains(e.NewTextValue));
                    else
                        registeredstdslist.ItemsSource = list_filtered_reg_std.Where(x => x.name.ToLower().Contains(e.NewTextValue) || x.stdid.ToLower().Contains(e.NewTextValue) || x.edlevel.ToString().ToLower().Contains(e.NewTextValue));

                }
            }
}

this is my xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ALNahrainAlphaApp.Accounting"
             xmlns:images="clr-namespace:ALNahrainAlphaApp;assembly=ALNahrainAlphaApp">
    <ContentPage.ToolbarItems >

        <ToolbarItem Order="Secondary"
                     Text="logout"
                     Priority="2"
                Clicked="ToolbarItem_Clicked"
                    />
    </ContentPage.ToolbarItems>
    <ContentPage.Resources>
        <Style TargetType="Grid">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal" />
                        <VisualState x:Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor"
                                        Value="#f4f0ec" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
    </ContentPage.Resources>
    <ContentPage.Content>

        <AbsoluteLayout Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <StackLayout BackgroundColor="White" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
                
                <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" >
                    <SearchBar HorizontalOptions="FillAndExpand" x:Name="search"  TextChanged="search_TextChanged"/>
                    <RefreshView x:Name="refresh" >
                        <CollectionView x:Name="registeredstdslist" SelectionChanged="registeredstdslist_SelectionChanged" SelectionMode="Single" >
                            <CollectionView.Header>
                                <Grid Padding="2" ColumnSpacing="1" RowSpacing="1">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="35"/>

                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="0.4*"/>
                                        <ColumnDefinition Width="0.5*"/>
                                        <ColumnDefinition Width="0.5*"/>
                                        <ColumnDefinition Width="0.3*"/>
                                        <ColumnDefinition Width="0.2*"/>
                                        <ColumnDefinition Width="0.3*"/>
                                    </Grid.ColumnDefinitions>
                                    <Label 
                                Grid.Column="0"
                                Text="StdID"
                                 TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                               />
                                    <Label 
                                Grid.Column="1" 
                                Text="Name"
                                 TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                />
                                    <Label 
                                Grid.Column="2"
                                Text="Reg Date"
                                 TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                />
                                    <Label
                                Grid.Column="3"
                                Text="Edlevel"
                                 TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                />
                                    <Label
                                Grid.Column="4"
                                Text="Grade"
                                 TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                />
                                    <Label
                                Grid.Column="5"
                                Text="Status"
                                 TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                />

                                </Grid>

                            </CollectionView.Header>
                            <CollectionView.ItemTemplate>
                                <DataTemplate >
                                    <Grid ColumnSpacing="1" RowSpacing="0" Padding="9,0,0,0" >
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="40"/>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="0.4*"/>
                                            <ColumnDefinition Width="0.5*"/>
                                            <ColumnDefinition Width="0.5*"/>
                                            <ColumnDefinition Width="0.3*"/>
                                            <ColumnDefinition Width="0.2*"/>
                                            <ColumnDefinition Width="0.3*"/>

                                        </Grid.ColumnDefinitions>
                                        <Label 
                                    Grid.Column="0"
                                    Text="{Binding stdid}"
                                    TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                        FontSize="13"
                                    />
                                        <Label 
                                    Grid.Column="1"
                                    Text="{Binding name}"
                                    TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                        FontSize="13"
                                    />
                                        <Label 
                                    Grid.Column="2"
                                    Text="{Binding DateofReg}"
                                    TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                        FontSize="13"
                                       
                                    />
                                        <Label 
                                    Grid.Column="3"
                                    Text="{Binding edlevel}"
                                    TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                        FontSize="13"
                                    />
                                        <Label 
                                    Grid.Column="4"
                                    Text="{Binding grade}"
                                    TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                        Padding="10"
                                        FontSize="13"
                                    />
                                        <Image
                                    
                                    Grid.Column="5"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    HeightRequest="20"
                                    WidthRequest="20"
                                   >
                                            <Image.Triggers>
                                                <DataTrigger TargetType="Image" Binding="{Binding status}" Value="false" >
                                                    <Setter Property="Source" Value="{OnPlatform Android=redtik.png,iOS=redtik.png}"/>
                                                </DataTrigger>
                                                <DataTrigger TargetType="Image" Binding="{Binding status}" Value="true" >
                                                    <Setter Property="Source" Value="{OnPlatform Android=greentik.png, iOS=greentik.png}"/>

                                                </DataTrigger>
                                            </Image.Triggers>
                                        </Image>

                                    </Grid>
                                </DataTemplate>
                            </CollectionView.ItemTemplate>
                        </CollectionView>
                    </RefreshView>
                    <StackLayout VerticalOptions="EndAndExpand"  HorizontalOptions="FillAndExpand" Orientation="Horizontal" BackgroundColor="#0d98ba">
                        <StackLayout Style="{StaticResource ButtonNavigationBarStackLayoutStyle}" x:Name="stckAddStd">
                            <Image Margin="0,10,0,5" x:Name="imgAdd" Style="{StaticResource ButtonNavigationBarImageStyle}" />
                            
                        </StackLayout>
                        <StackLayout Style="{StaticResource ButtonNavigationBarStackLayoutStyle}" x:Name="stckfilter">
                            <Image Margin="0,10,0,10" x:Name="imgfilter" Style="{StaticResource ButtonNavigationBarImageStyle}" />
                            
                        </StackLayout>
                        <StackLayout Style="{StaticResource ButtonNavigationBarStackLayoutStyle}" x:Name="stckshare">
                            <Image Margin="0,10,0,10" x:Name="imgshare" Style="{StaticResource ButtonNavigationBarImageStyle}" />
                            
                        </StackLayout>

                    </StackLayout>
                </StackLayout>
            </StackLayout>
            <ContentView x:Name="popupLoadingView" BackgroundColor="Transparent" Padding="10, 0" IsVisible="false"  AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
                <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
                    <StackLayout Orientation="Vertical" HeightRequest="150" WidthRequest="200" BackgroundColor="Transparent">

                        <ActivityIndicator x:Name="activityIndicator" Margin="0,50,0,0" VerticalOptions="Center" HorizontalOptions="Center" Color="Black" WidthRequest="40" HeightRequest="40" ></ActivityIndicator>
                    </StackLayout>
                </StackLayout>
            </ContentView>
        </AbsoluteLayout>
    </ContentPage.Content>
   
</ContentPage>

the registeredstdslist is a collection view that has data about students that i get from a database. the code works well on android but on my ios simulator, it doesn't work. the collectionview shows no data when i write something in the searchbar though what i write has letters like some data in my list. and when i erase what i wrote, all data are displayed again. why is that? what am i doing wrong?

Update: i added this:

private void search_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (list_total_reg_std.Count != 0)
            {
                if (string.IsNullOrEmpty(e.NewTextValue))
                {
                    if (filtered == false)
                        registeredstdslist.ItemsSource = list_total_reg_std;
                    else
                        registeredstdslist.ItemsSource = list_filtered_reg_std;
                }

                else
                {
                    if (filtered == false)
                        if(list_total_reg_std.FindAll(x => x.name.ToLower().Contains(e.NewTextValue)).Count==0)
                            DisplayAlert("Operation Failed", "No matchiing data!", "Cancel");

                   // registeredstdslist.ItemsSource = list_total_reg_std.Where(x => x.name.ToLower().Contains(e.NewTextValue) || x.stdid.ToLower().Contains(e.NewTextValue) || x.edlevel.ToString().ToLower().Contains(e.NewTextValue));
                    else
                        registeredstdslist.ItemsSource = list_filtered_reg_std.Where(x => x.name.ToLower().Contains(e.NewTextValue) || x.stdid.ToLower().Contains(e.NewTextValue) || x.edlevel.ToString().ToLower().Contains(e.NewTextValue));

                }
            }

it actually gave No matching data but why? and why does it work on android with no problems and on ios it doesn't?

CodePudding user response:

One problem I see is that if you type any upper case, it will not match.

x.name.ToLower().Contains(e.NewTextValue) should be x.name.ToLower().Contains(e.NewTextValue.ToLower()).

  • Related