0

Xamarin Forms C# ListView Search
Hello everyone, i want to make a search from ListView when i make filter by name i want suggestions appear in listview I tried to do this but not working for me !! i don't understand what is the problem..

Screenshot for the problem: https://i.stack.imgur.com/OaRas.jpg

Xamarin Forms C# ListView Search   

Hello everyone, i want to make a search from ListView when i make filter by name i want suggestions appear in listview I tried to do this but not working for me !! i don't understand what is the problem..

    Model> echeance.cs:
    ------------

    namespace Appz.Model
    {
        public class Echeance
        {

            [PrimaryKey, AutoIncrement]
            public int ChkId { get; set; }

            public string ChkChoise { get; set; }

            [MaxLength(50)]
            public string ChkNumber { get; set; }

            public string ChkAmount { get; set; }

            [MaxLength(50)]
            public string BeneficiaryName { get; set; }

            public string AgencyType { get; set; }

            public DateTime ChkDueDate { get; set; }

            public DateTime ChkReleaseDate { get; set; }

            public string ChkImage { get; set; }


        }
    }





    EcheanceView.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"
                  xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" 
        ios:Page.UseSafeArea="true"
                 x:Class="Appz.View.EcheanceView">
        <ContentPage.Content>
            <StackLayout Padding="10,0,10,0">
                <SearchBar TextChanged="Handle_TextChanged"></SearchBar>
                <ListView  ItemsSource="{Binding EcheancesList}" IsPullToRefreshEnabled="True"  x:Name="ListEcheances" HasUnevenRows="true" SeparatorVisibility="Default" ItemTapped="OnItemSelected">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Orientation="Horizontal">
                                    <Image Source="{Binding ChkImage}" HeightRequest="50" WidthRequest="50"  />
                                    <StackLayout HorizontalOptions="StartAndExpand">
                                        <Label Text="{Binding BeneficiaryName}"
                                           FontAttributes="Bold" />
                                        <Label Text="{Binding AgencyType}"
                                           TextColor="Gray" />
                                    </StackLayout>
                                    <Button Text="Follow Me"
                                        BorderColor="Silver"
                                       FontSize="Small"
                                       TextColor="White"
                                       BackgroundColor="#3399ff"
                                       VerticalOptions="Center"
                                      />
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </StackLayout>

        </ContentPage.Content>
    </ContentPage>





    EcheanceView.cs:
    ---------------

    void Handle_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
            {


                using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
                {               
                    var echeances = conn.Table<Echeance>().ToList();
                    //ListEcheances.ItemsSource = echeances;

                    if (string.IsNullOrWhiteSpace(e.NewTextValue))
                        ListEcheances.ItemsSource = echeances;
                    else
                        ListEcheances.ItemsSource = echeances.Where(i => i.BeneficiaryName.Contains(e.NewTextValue));

                    ListEcheances.EndRefresh();

                }                    

            }

someone help me please Thanks in advance !!!

progra006
  • 1
  • 2
  • "not working" is not a helpful explanation. Is it crashing or generating a compiler error? Is it doing anything at all? Have you used the debugger to verify that your events are firing and your code is executing as expected? – Jason Apr 29 '19 at 22:25
  • Screen: https://i.imgur.com/vhpt8Cp.jpg – progra006 Apr 29 '19 at 22:36
  • 1
    please DO NOT post code and errors as images – Jason Apr 29 '19 at 22:41
  • you are getting a null ref exception - something in that statement is null. You need to figure out which element and either fix it or add code to avoid it. – Jason Apr 29 '19 at 22:42
  • i don't understand what you mean ? – progra006 Apr 29 '19 at 22:46
  • 2
    https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Jason Apr 29 '19 at 22:51
  • Put in the else case ListEcheances.ItemsSource = echeances.Where(i => i.BeneficiaryName.Contains(e.NewTextValue)).ToList(); – Anand Apr 30 '19 at 04:20
  • I think `BeneficiaryName` is null in some rows. Also, don't change the `ItemSource` just clear-and-add to the existing ObservableCollection – memsranga Apr 30 '19 at 06:24

2 Answers2

0

The approach I would go with for real-time filtering, is having two data sets, a backing list and a filtered collection that dynamically changes (this is the one you should bind your ListView's ItemSource to). Then just bind your Entry's Text property to your corresponding property (SearchTerm in this example). In the setter you could execute your filter/search command.

  private ObservableCollection<Test> filteredItems;
    public ObservableCollection<Test> FilteredItems
    {
        get => filteredItems;
        set
        {
            // Notify property changed
        }
    }

    private List<Test> allItems;
    public List<Test> AllItems
    {
        get => allItems;
        set
        {
            // Notify property changed
        }
    }

    private string searchTerm;
    public string SearchTerm
    {
        get => searchTerm;
        set
        {
            // Notify property changed
            SearchCommand.Execute(searchTerm);
        }
    }

    public Command SearchCommand
    {
        get
        {
            return new Command<string>((searchString) =>
            {
                FilteredItems = new ObservableCollection<Test>(AllItems.Where(o => o.itemText.ToLower().Contains(searchString.ToLower())));

            });
        }
    }
jakelli
  • 23
  • 7
0

In your case it seems that some items have BeneficiaryName null.

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57