Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Search Query in SearchBar Working in IOS But Not in Android

ziggyziggy Member ✭✭

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}"

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    I've reproduced the issue on Android. You can refer to it.
    page.xaml.cs

        public partial class MainPage : ContentPage
        {
            List<string> country = new List<string>
            {
                "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

    <StackLayout>
        <SearchBar x:Name="search_bar"
                       Placeholder="Name or Keyword" 
                       Text="{Binding SearchTerm, Mode=TwoWay}"
                       TextChanged="Search_bar_TextChanged"/>
        <ListView x:Name="list"/>
    </StackLayout>
    
  • ziggyziggy Member ✭✭

    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

  • JarvanJarvan Member, Xamarin Team Xamurai

    Have a look at the code.

    page.xaml.cs

        public partial class Page1 : ContentPage
        {
            ObservableCollection<People> list = new ObservableCollection<People>();
    
            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

    <StackLayout>
        <Label Text="Welcome to Xamarin.Forms!"
                    HorizontalOptions="CenterAndExpand" />
        <SearchBar x:Name="search_bar"
                       Placeholder="Name or Keyword" 
                       Text="{Binding SearchTerm, Mode=TwoWay}"
                       TextChanged="Search_bar_TextChanged"/>
        <ListView x:Name="peopleList">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell Height="30">
                        <StackLayout Padding="5">
                            <Label Text="{Binding Name}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
    

    model.cs

        public class People
        {
            public string Name { get; set; }
            public People()
            {
    
            }
        }
    
Sign In or Register to comment.