Home > Enterprise >  I could not display the employee's name, tasks, and date via the ListView(Xamarin.Forms)
I could not display the employee's name, tasks, and date via the ListView(Xamarin.Forms)

Time:11-17

I made two tables, the first stores the names of the employees, and the second stores the duties and the date. But I could not display the name of the employee, the duties assigned to him, and the date. This is my code, hope you can help me.

public class MyTabels
{
    [Table("Employee")]
    public class Employee
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        public string Name { get; set; }
        public string LastName { get; set; }

        [OneToMany]
        public List<Duty> Duties { get; set; }
    }


    [Table("Duties")]
    public class Duty
    {

        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        public string Description { get; set; }

        public DateTime Deadline { get; set; }

        [ForeignKey(typeof(Employee))]
        public int EmployeeId { get; set; }
    }
}

This XAML There is an CollectionView and I have added items into it:

<CollectionView x:Name="CV" IsGrouped="True">
            <CollectionView.HeaderTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                        <Label Text="{Binding Name}" FontSize="Large"/>
                        <Label Text="{Binding LastName}" FontSize="Large"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </CollectionView.HeaderTemplate>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label Text="{Binding Description}" FontSize="Medium"/>
                            <Label Text="{Binding Deadline}" FontSize="Medium"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

This My Code:

var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Mydb.db");
        var db = new SQLiteConnection(dbpath);
        db.CreateTable<Employee>();
        db.CreateTable<Duty>();
var employee = new Employee
        {
            Name = "Andrew",
            LastName = "Programmer"
        };
        db.Insert(employee);


        var duty1 = new Duty()
        {
            Description = "Project A Management",
            Deadline = new DateTime(2017, 10, 31)
        };

        var duty2 = new Duty()
        {
            Description = "Reporting work time",
            Deadline = new DateTime(2022, 12, 31)
        };

        db.Insert(duty1);
        db.Insert(duty2);

        employee.Duties = new List<Duty> { duty1, duty2 };
        db.UpdateWithChildren(employee);

        var employeeStored = db.GetWithChildren<Employee>(employee.Id);

        CV.ItemsSource = employeeStored.Duties;

CodePudding user response:

Make a new class to convert the data you get from the database match the CollectionView ItemSource.

Model:

public class EmployeeList : List<Duty>
{
    public string Name { get; set; }
    public string LastName { get; set; }
    public EmployeeList(string name, string lastName, List<Duty> duties) : base(duties)
    {
        Name = name;
        LastName = lastName;
    }
}

Usage: Use GetAllWithChildren to get all the employees into list from the database.

       public partial class Page3 : ContentPage
{
    public List<EmployeeList> employeeStored { get; set; } = new List<EmployeeList>();

    public Page3()
    {
        InitializeComponent();

        var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Mydb.db");
        var db = new SQLiteConnection(dbpath);
        db.CreateTable<Employee>();
        db.CreateTable<Duty>();
        var employee = new Employee
        {
            Name = "Andrew",
            LastName = "Programmer"
        };
        db.Insert(employee);


        var duty1 = new Duty()
        {
            Description = "Project A Management",
            Deadline = new DateTime(2017, 10, 31)
        };

        var duty2 = new Duty()
        {
            Description = "Reporting work time",
            Deadline = new DateTime(2022, 12, 31)
        };

        db.Insert(duty1);
        db.Insert(duty2);

        employee.Duties = new List<Duty> { duty1, duty2 };
        db.UpdateWithChildren(employee);

        //var employeeStored2 = db.GetWithChildren<Employee>(employee.Id);

        var employeeStored = db.GetAllWithChildren<Employee>();

        
        var list = Convert(employeeStored);
        CV.ItemsSource = list;
        this.BindingContext = this;

    }
    public List<EmployeeList> Convert(List<Employee> employees)
    {
        foreach (var item in employees)
        {
            employeeStored.Add(new EmployeeList(item.Name, item.LastName, item.Duties));
        }
        return employeeStored;

    }


}

And note that CollectionView has no concept of cells. So delete it in your Xaml.

Update:

Xaml:

      <CollectionView  x:Name="CV" IsGrouped="True">
        <CollectionView.GroupHeaderTemplate>
            <DataTemplate>
               
                    <StackLayout>
                        <Label Text="{Binding  Name}" FontSize="Large"/>
                        <Label Text="{Binding LastName}" FontSize="Large"/>
                    </StackLayout>
                 
            </DataTemplate>
        </CollectionView.GroupHeaderTemplate>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Label Text="{Binding Description}" FontSize="Medium"/>
                    <Label Text="{Binding Deadline}" FontSize="Medium"/>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
  • Related