简体   繁体   中英

SQLite-Net populate ListView partially

I'm using SQLite-Net-PCL to read and write data to a local SQLite database. On one page of my app I need all entries from a specific table that match a query. I also need to retrieve the child entities with relationships to the parent model. Therefore I use SQLite-Net Extensions . Fetching the items on a mobile device can take some time.

As the ListView can't show all entries anyway - I want to load ie the first 100 entries and than all the rest when the user scrolls the ListView .

I wrote a DatabaseHelper which provides the following method:

public async static Task<ObservableCollection<Item>> GetItemsByQueryAsync(string query)
{
    List<Item> models = null;
    var conn = new SQLiteAsyncConnection(() => getAsyncSQLiteConnection());
    models = await conn.GetAllWithChildrenAsync<Item>(i => i.name.Contains(query));
    return new ObservableCollection<Item>(models);
}

I call this method from my ViewModel to create the ItemsSource for the ListView :

public ObservableCollection<Item> Items

private async void GetItems()
{
    Items = await DatabaseHelper.GetItemsByQueryAsync(SearchQuery);
}

This is the XAML code for the ListView (I removed the template code)

<ListView x:Name="Items_ListView" ItemsSource="{Binding Items}" />

What you are looking for is implementing the ISupportIncrementalLoading interface that is build in the framework ( https://msdn.microsoft.com/library/windows/apps/Hh701916 )

A good example can be found here: https://marcominerva.wordpress.com/2013/05/22/implementing-the-isupportincrementalloading-interface-in-a-window-store-app/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM