Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

c# - Dynamically create ListView rows from SQL Lite DB

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

use LINQ Take to limit the number of results

return _database.Table<Person>().Take(5).ToListAsync();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...