locked
Search Query in SearchBar Working in IOS But Not in Android RRS feed

  • Question

  • User382837 posted

    I created a sample Pet List with a Search View at the very top. The Search View would help user filter results of the List. The implementation works in IOS but I get an error in Android.

    in IOS:

    in Android:

    Here is How I implemented it: 1. I created a view model with the SearchItem as one of the properties public class PetsViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private IList _pets; private IList originalList; private int _current; private string _searchTerm;

        public string SearchTerm {
            get => _searchTerm;
            set
            {
                if (_searchTerm != value)
                {
                    _searchTerm = value;
                    RaisePropertyChanged();
                    if (string.IsNullOrWhiteSpace(_searchTerm))
                    {// if search string is null List to original list
                        Pets = originalList;
                    }
                    else
                    {// show data that has filtered data
                        var data = Pets.Where(i => i.Name.Contains(_searchTerm));
                        Pets = (IList<Pet>)data;
                    }
                }
            }
    
        }
    
    1. in XAML I used binding with the SearchBar

      SearchBar Placeholder="Name or Keyword" Text="{Binding SearchTerm, Mode=TwoWay}"

    Tuesday, March 26, 2019 5:37 PM

All replies

  • User382871 posted

    I've reproduced the issue on Android. You can refer to it. page.xaml.cs ``` public partial class MainPage : ContentPage { List country = new List { "India", "pakistan", "Srilanka", "Bangladesh", "Afghanistan" }; public MainPage() { InitializeComponent(); list.ItemsSource = country; }

        private void Search_bar_TextChanged(object sender, TextChangedEventArgs e)
        {
            var keyword = search_bar.Text;
    
                if (keyword.Length >= 1)
                {
                    var suggestion = country.Where(c => c.ToLower().Contains(keyword.ToLower()));
                    list.ItemsSource = suggestion;
                    list.IsVisible = true;
                }
                else
                {
                    //list.IsVisible = false;
                }
        }
    }
    

    page.xaml ```

    Wednesday, March 27, 2019 9:03 AM
  • User382837 posted

    Actually, I did the same implementation as you by moving the implementation to an Event. I wanted to see if it was possible to put it in the View Model

    Wednesday, March 27, 2019 9:17 AM
  • User382871 posted

    Have a look at the code.

    page.xaml.cs ``` public partial class Page1 : ContentPage { ObservableCollection list = new ObservableCollection();

        List<string> list1 = new List<string>();
    
        public Page1 ()
        {
            InitializeComponent ();
    
            list.Add(new People { Name="jack" });
            list.Add(new People { Name = "jarvan" });
    
            peopleList.ItemsSource = list;
        }
    
        private void Search_bar_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.NewTextValue))
            {
                peopleList.ItemsSource = list;
            }
            else
            {
                peopleList.ItemsSource = list.Where(i => i.Name.Contains(e.NewTextValue));
            }
        }
    }
    

    page.xaml model.cs public class People { public string Name { get; set; } public People() {

        }
    }
    

    ```

    Friday, March 29, 2019 2:11 AM