I wish to create an app whereby a ListView row is automatically created based on an SQL DB entry - up to a maximum of 5 ListView rows.
Person class:
using SQLite;
namespace AppPeople
{
public class Person
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
Database.cs:
using System.Collections.Generic;
using System.Threading.Tasks;
using SQLite;
namespace AppPeople
{
public class Database
{
readonly SQLiteAsyncConnection _database;
public Database(string dbPath)
{
_database = new SQLiteAsyncConnection(dbPath);
_database.CreateTableAsync<Person>().Wait();
}
public Task<List<Person>> GetPeopleAsync()
{
return _database.Table<Person>().ToListAsync();
}
public Task<int> SavePersonAsync(Person person)
{
return _database.InsertAsync(person);
}
}
}
MainPage.xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="AppPeople.MainPage">
<ListView x:Name="saved_properties_list">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal"
HorizontalOptions="FillAndExpand"
Margin="20, 10, 20, 0" >
<Label Text="{Binding Name}" HorizontalOptions="StartAndExpand" />
<Label Text="{Binding Age}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
MainPage.cs:
protected override async void OnAppearing()
{
base.OnAppearing();
saved_properties_list.ItemsSource = await MainPage.Database.GetPropertiesAsync();
}
public Task> GetPropertiesAsync()
{
return database.Table().ToListAsync();
}
How would I go about changing this so that a ListView row is automatically created based on an SQL DB entry with up to a maximum of 5 ListView rows? Thank you.
question from:
https://stackoverflow.com/questions/65922869/dynamically-create-listview-rows-from-sql-lite-db 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…