Search code examples
androidxamarinmvvmcrosssearchview

Search from List which is in ViewModel (Xamarin Android )


I am new to Xamarin Android Development and I am using MvvmCross for binding data.I have SerachView on action-bar.I want to search data from list which is in ViewModel.How can I implement that ? I have searched for this issue on internet but all have used adapter and i want to search list-item without using adapter from ViewModel.I am not getting any idea how to do that.So anyone can suggest me an easy way?

Any suggestion or advice will be appreciated.


Solution

  • It is pretty simple.

    Your SearchView is bound to a string property which you are using for filtering. Here I assume it is called SearchQuery.

    It is not clear what criteria you want to use for filtering, I will assume that the ViewModel has a Name property, where the SearchQuery will be contained in that name.

    So your ViewModel would look something like:

    public class SearchViewModel : MvxViewModel
    {
        public string SearchQuery
        {
            get { return _searchQuery; }
            set {
                _searchQuery = value;
                RaisePropertyChanged(() => SearchQuery);
                RaisePropertyChanged(() => FilteredResults);
            }
        }
    
        public List<ListItemViewModel> UnfilteredResults 
        {
            get { return _unfilteredResults; }
            set {
                _unfilteredResults = value;
                RaisePropertyChanged(() => UnfilteredResults);
                RaisePropertyChanged(() => FilteredResults);
            }
        }
    
        public List<ListItemViewModel> FilteredResults
        {
            get 
            {
                if (string.IsNullOrEmpty(SearchQuery)) 
                    return UnfilteredResults;
    
                return UnfilteredResults
                    .Where(r => r.Name.Contains(SearchQuery)).ToList();
            }
        }
    }
    

    So what happens is, whenever you enter a new value into the search box, it will trigger the PropertyChanged event on FilteredResults and use a simple LINQ query to filter the results.

    If you don't want to swap out the entire list every time, you can do this with an ObservableCollection and add and remove items in that instead.

    EDIT:

    So as stated above you just bind the MvxListView to the new items source. Assuming you are using a AXML layout for your view:

    <MvxListView
      ..
      local:MvxBind="ItemsSource FilteredResults; ItemClick ResultClickedCommand" />
    

    As for the SearchView, I just looked, there does not seem to be any code in MvvmCross to easily bind to that, and it does not inherit from EditText, so you need to do something like described here: https://stackoverflow.com/a/22501906/368379

    Easiest way is probably simply to implement the SearchView.IOnQueryTextListener interface and in there set the new string you receive in the implementation on your ViewModel.