Forum Xamarin Xamarin.Forms

how to filter a listview ?

delaiodelaio Member ✭✭

Hello,

I get a list of appointments with their date and time via an API.

According to the user's choice (see the list of future meetings / see the list of past meetings) I want to filter by date and time but I do not know the best approach. Must:

  • filter listappointment_lv listview ?
  • filter ResponseAPIAppointments object ?
  • create a Appointments class and assign the filtered result to it ?

Regarding the full functionality: I have to display the list of appointments by sorting the date and time in descending order and when I click on an appointment I display a new page with the details of the appointment.

Thank for your advices.

API response

    public class ResponseAPIAppointments
    {
        public class Services
        {
            public string libelle { get; set; }
            public string speaker { get; set; }
        }

        public class Appointments
        {
            public int? id { get; set; }
            public string name { get; set; }
            public int? number { get; set; }
            public DateTime? expirationdate { get; set; }
            public string expirationheure { get; set; }
            public List<Services_cl> services { get; set; }
        }

        [JsonProperty("status")]
        public string status_st { get; set; }

        [JsonProperty("success")]
        public bool Success_bo { get; set; }

        [JsonProperty("error")]
        public string Error_st { get; set; }

        [JsonProperty("Appointments")]
        public List<Appointments> AppointmentList { get; set; }
    }

xaml

                        <ListView x:Name="listappointment_lv"
                                  ItemsSource="{Binding ResponseAppointments.AppointmentList}"
                                  Grid.Row="1"
                                  HasUnevenRows="true">
                            <ListView.ItemTemplate>
                                <DataTemplate>                                     
                                    <ViewCell>
                                        <Label Padding="10"
                                               HorizontalTextAlignment="Center" 
                                               TextColor="SteelBlue"
                                               FontSize="Body">
                                            <Label.FormattedText>
                                                <FormattedString>
                                                    <Span Text="{Binding expirationdate , StringFormat='{0:MM/dd/yyyy}'}}" />
                                                    <Span Text=" " />
                                                    <Span Text="{Binding expirationheure}" />
                                                </FormattedString>
                                            </Label.FormattedText>
                                        </Label>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

xaml.cs

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page : ContentPage
    {
        private ResponseAPIAppointments _responseAppointments ;
        public ResponseAPIAppointments ResponseAppointments 
        {
            set  {  _responseAppointments = value; OnPropertyChanged();  }
            get => _responseAppointments ;
        }

        public Page(string pastfuture)
        {
            InitializeComponent();
       // call API
       // ResponseAppointments object fill

            BindingContext = this;
        }

Posts

  • JohnHardmanJohnHardman GBUniversity admin
    edited October 16

    @delaio

    Create a new ObservableCollection property in your ViewModel. Have that initially populated with the data from ResponseAppointments.AppointmentList . When the user changes the filter or sort criteria, have your ViewModel update that new collection by filtering/sorting data from AppointmentList. Change the binding of ListView.ItemsSource to bind to that new property.

  • delaiodelaio Member ✭✭

    thank you John for this answer.

  • JohnHardmanJohnHardman GBUniversity admin

    @delaio said:
    thank you John for this answer.

    If you find answers helpful, don't forget to Like them (and if they were asked as Questions rather than Discussions, don't forget to mark them as Accepted Answer) :-)

Sign In or Register to comment.