nor0x
nor0x

Reputation: 1213

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 i.e. 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}" />

Upvotes: 1

Views: 789

Answers (1)

Depechie
Depechie

Reputation: 6142

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/

Upvotes: 3

Related Questions