Talia Dianal
Talia Dianal

Reputation: 115

two list view in the same view in Xamarin

I'm trying to show and get two list view in xamarin app in order to get data from firebase database, so i have to have two binding on it i trace MVVM design pattern and this is what i have

in view model page :

public class HomePageModelView : BaseViewModel
    {
        private ObservableCollection<Appoitment> _Appoitments = new ObservableCollection<Appoitment>();

        public ObservableCollection<Appoitment> Appoitments { get => _Appoitments; set => SetProperty(ref _Appoitments, value, nameof(Appoitments)); }

        private ObservableCollection<Appoitment> _Appoitments2 = new ObservableCollection<Appoitment>();

        public ObservableCollection<Appoitment> Appoitments2 { get => _Appoitments2; set => SetProperty(ref _Appoitments2, value, nameof(Appoitments2)); }

        private ICommand _Appearing;

        public ICommand Appearing { get => _Appearing; set => SetProperty(ref _Appearing, value, nameof(Appearing)); }

        public HomePageModelView()
        {

            Appearing = new AsyncCommand(async () => await LoadData());
            Appearing = new AsyncCommand(async () => await LoadData2());

            var now = DateTime.Now.ToString();
            var x = JsonConvert.SerializeObject(DateTime.Now);
            Console.WriteLine(x);
        }

        async Task LoadData()
        {
            Appoitments = new ObservableCollection<Appoitment>(await AppintmentService.GetUserAppointments());

        }
        async Task LoadData2()
        {
            Appoitments2 = new ObservableCollection<Appoitment>(await AppintmentService.GetUserTAppointments());

        }


    }

in View page :

<StackLayout>
    <Label Text = "Today Appointment" TextColor = "#2196f3"   WidthRequest = "200"
               HeightRequest="50"  FontSize = "Medium" Margin = "13" FontAttributes = "Bold" ></Label >
    <ListView HasUnevenRows="True" ItemsSource="{Binding Appoitments}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>

                    <Grid RowDefinitions="Auto,Auto,Auto">
                        <Label Grid.Row="0" Text="{Binding AppointmentPatientName}"/>
                        <Label Grid.Row="0" Grid.Column="1" Text="{Binding AppointmentDate}" />

                    </Grid>

                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Label Text = "Tommorrow Appointment" TextColor = "#2196f3"   WidthRequest = "200"
               HeightRequest="50"  FontSize = "Small" Margin = "13" FontAttributes = "Bold" ></Label >
    <ListView HasUnevenRows="True" ItemsSource="{Binding Appoitments2}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>

                    <Grid RowDefinitions="Auto,Auto,Auto">
                        <Label Grid.Row="0" Text="{Binding AppointmentPatientName}"/>
                        <Label Grid.Row="0" Grid.Column="1" Text="{Binding AppointmentDate}" />

                    </Grid>

                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

But what i take is just the second part (Tommorrow appointment) and no data in first one There's no errors on exception on it, just data not loaded.. anyone knows where is the problem ?

Upvotes: 0

Views: 113

Answers (1)

Jason
Jason

Reputation: 89082

you are assigning Appearing twice, so the 2nd assignment overwrites the first one

Appearing = new AsyncCommand(async () => await LoadData());
Appearing = new AsyncCommand(async () => await LoadData2());

do this instead

Appearing = new AsyncCommand(async () => 
{
  await LoadData());
  await LoadData2();
});

Upvotes: 2

Related Questions