mercredi 7 octobre 2015

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}" />

Aucun commentaire:

Enregistrer un commentaire