Home > Mobile >  Getting all objects from another object based on it's id
Getting all objects from another object based on it's id

Time:01-07

I have 2 classes

Terms

 public class Terms
    {
        [PrimaryKey, AutoIncrement]
        public int TermId { get; set; }
        public string TermName { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public DateTime RealStart => DateTime.Parse(StartDate);
        public DateTime RealEnd => DateTime.Parse(EndDate);
    }

Courses

public class Courses
    {
        [PrimaryKey, AutoIncrement]
        public int CourseId { get; set; }
        public int TermId { get; set; }
        public string CourseName { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public DateTime RealStart => DateTime.Parse(StartDate);
        public DateTime RealEnd => DateTime.Parse(EndDate);
        public string CIName { get; set; }
        public string CIEmail { get; set; }
        public string CIPhone { get; set; }
        public string Status { get; set; }
        public bool Notifications { get; set; }
        public string Notes { get; set; }
    }

Term 1 - Course 1 Course 2

When selecting a course from Term 1, I'm trying to display the expanded course details for that course.

xaml

<CollectionView x:Name="CourseCollection"
                                        ItemsSource="{Binding Courses}"
                                        EmptyView="No Courses to display"
                                        SelectionMode="Single"
                                        SelectionChanged="CourseCollection_SelectionChanged">

C#

private async void CourseCollection_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var course = (Courses)e.CurrentSelection.FirstOrDefault();
            if (e.CurrentSelection != null)
            {
                await Navigation.PushAsync(new CoursesDetail(course));
            }
        }

It's displaying both courses

CoursesDetail

xaml

<CollectionView x:Name="CoursesCollection"
                                        ItemsSource="{Binding Courses}"
                                        EmptyView="No courses to view"
                                        SelectionMode="Single">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout>
                            <Grid Padding="5" RowSpacing="1" ColumnSpacing="10">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>

                                <Label Grid.Row="0" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Course ID</Label>
                                <Label Text="{Binding CourseId}" Grid.Row="0" Grid.Column="1" FontSize="Medium" x:Name="CourseId" ></Label>

                                <Label Grid.Row="1" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Term ID</Label>
                                <Label  Text="{Binding TermId}" Grid.Row="1" Grid.Column="1" FontSize="Medium" x:Name="TermId"></Label>

                                <Label Grid.Row="2" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Course Name</Label>
                                <Label x:Name="CourseName"  Grid.Row="2" Grid.Column="1" FontSize="Medium" VerticalTextAlignment="Center" Text="{Binding CourseName}"></Label>

                                <Label Grid.Row="3" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Start Date</Label>
                                <Label Grid.Row="3" Grid.Column="1" FontSize="Medium" x:Name="StartDate" Text="{Binding StartDate}"></Label>

                                <Label Grid.Row="4" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">End Date</Label>
                                <Label Grid.Row="4" Grid.Column="1" FontSize="Medium" x:Name="EndDate" Text="{Binding EndDate}"></Label>

                                <Label Grid.Row="5" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Instructor Name</Label>
                                <Label x:Name="CIName" Grid.Row="5" Grid.Column="1" FontSize="Medium" VerticalTextAlignment="Center" Text="{Binding CIName}"></Label>

                                <Label Grid.Row="6" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Instructor Email</Label>
                                <Label x:Name="CIEmail" Grid.Row="6" Grid.Column="1" FontSize="Medium" VerticalTextAlignment="Center" Text="{Binding CIEmail}"></Label>

                                <Label Grid.Row="7" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Instructor Phone</Label>
                                <Label x:Name="CIPhone" Grid.Row="7" Grid.Column="1" FontSize="Medium" VerticalTextAlignment="Center" Text="{Binding CIPhone}"></Label>

                                <Label Grid.Row="8" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Course Status</Label>
                                <Label Grid.Row="8" Grid.Column="1" x:Name="Status" FontSize="Medium" Text="{Binding Status}"></Label>

                                <Label Grid.Row="9" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Notes</Label>
                                <Label x:Name="Notes" Grid.Row="9" Grid.Column="1" FontSize="Medium" Text="{Binding Notes}"></Label>

                                <Label Grid.Row="10" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Notifications</Label>
                                <Label x:Name="Notifications" Grid.Row="10" Grid.Column="1" FontSize="Medium" Text="{Binding Notifications}"></Label>
                            </Grid>
                        </StackLayout>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

c#

public partial class CoursesDetail : ContentPage
    {
        private readonly int _selectedCourseId;
        private Courses theBiggestCourses;

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            CoursesCollection.ItemsSource = await database.GetCourses(_selectedCourseId);
            AssessmentsCollection.ItemsSource = await database.GetAssessments(_selectedCourseId);
        }

        public CoursesDetail()
        {
            InitializeComponent();
        }

        public CoursesDetail(Courses courses)
        {
            _selectedCourseId = courses.CourseId;
            InitializeComponent();
            this.theBiggestCourses = courses;
        }

I've tried manually passing through each text field through the constructor. I have sample data that should auto populate and thought that it was the issue, but I removed those methods and tried manually added a Term, Courses for that Term but as soon as it gets to more than 1 course, it wacks out.

EDIT - My pea sized brain theory could be that since Terms is getting passed through as an object, capturing all the elements of that object, that it's still holding onto that Term object, so when calling the Courses object, it's capturing all the courses objects for that terms object. I hope I explained that decent.

EDIT2 - GET COURSES

 public static async Task<IEnumerable<Courses>> GetCourses(int termId)
        {
            await Init();

            var courses = await db.Table<Courses>()
                .Where(i => i.TermId == termId).ToListAsync();

            return courses;
        }

        public static async Task<IEnumerable<Courses>> GetCourses()
        {
            await Init();

            var courses = await db.Table<Courses>().ToListAsync();

            return courses;
        }

These are the two for GetCourses. Is it because we're passing through an int termId here and an object in the method through the constructor? Then it defaults to the unparameterized constructor, which is going to return all of them?

CodePudding user response:

GetCourses expects a TermID

public static async Task<IEnumerable<Courses>> GetCourses(int termId)
    {
        await Init();

        var courses = await db.Table<Courses>()
            .Where(i => i.TermId == termId).ToListAsync();

        return courses;
    }

but when you call it you are passing a CourseID

CoursesCollection.ItemsSource = await database.GetCourses(_selectedCourseId);
      
  • Related