Articles in this section
Category / Section

How to refresh ListView after dropping a table in Xamarin.Forms (SfListView)

2 mins read

You can refresh the SfListView when you drop the SQLite Database table in Xamarin.Forms. Refer to the document below to work with the SQLite database from here.

C#

Define the asynchronous method used to create, retrieve and drop the table.

public class SQLiteDatabase
{
    readonly SQLiteAsyncConnection _database;
 
    public SQLiteDatabase(string dbPath)
    {
        _database = new SQLiteAsyncConnection(dbPath);
    }
 
    public Task<CreateTableResult> CreateTableAsync()
    {
        return _database.CreateTableAsync<Contacts>();
    }
    …
    public Task<int> DropTableAsync()
    {
        return _database.DropTableAsync<Contacts>();
    }
}

C#

Load data from the SQLite database using commands. After dropping the table, clear the collection bounds to the ListView.

public class ContactsViewModel : INotifyPropertyChanged
{
    public SQLiteDatabase Database
    {
        get
        {
            if (database == null)
                database = new SQLiteDatabase(path);
            return database;
        }
    }
 
    public Command LoadDataCommand { get; set; }
    public Command DropTableCommand { get; set; }
 
    public ContactsViewModel()
    {
        LoadDataCommand = new Command(OnLoadData);
        DropTableCommand = new Command(OnDropTable);
    }
 
    private async void OnLoadData()
    {
        Random r = new Random();
        IsLoading = true;
        await Database.CreateTableAsync(); //Create Table
        for (int i = 0; i < 30; i++)
        {
            var contact = new Contacts();
            contact.ContactName = CustomerNames[r.Next(0, CustomerNames.Count())];
            contact.ContactNumber = r.Next(720, 799).ToString() + " - " + r.Next(3010, 3999).ToString();
            await Database.AddContactAsync(contact); // Add the item to the database table.
        }
 
        var items = await Database.GetContactsAsync(); // Retrieve data from the database table.
        ContactsInfo = new ObservableCollection<Contacts>(items);
        IsLoading = false;
    }
 
    private async void OnDropTable(object obj)
    {
        await Database.DropTableAsync();
        ContactsInfo.Clear(); // Clear the collection after dropping the table.
    }
}

XAML

Bind the ViewModel commands to the Command property of the TapGestureRecognizer.

<Grid>
    <Button Text="Load Data" Command="{Binding LoadDataCommand}" BackgroundColor="#ddf3f5"/>
    <syncfusion:SfListView x:Name="listView" ItemSize="60" FooterSize="50" ItemSpacing="5" ItemsSource="{Binding ContactsInfo}" IsStickyFooter="True" Grid.Row="1">
        <syncfusion:SfListView.ItemTemplate >
            <DataTemplate>
                ...
            </DataTemplate>
        </syncfusion:SfListView.ItemTemplate>
        <syncfusion:SfListView.FooterTemplate>
            <DataTemplate>
                <Grid BackgroundColor="Transparent">
                    <Grid.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding Path=BindingContext.DropTableCommand, Source={x:Reference listView}}"/>
                    </Grid.GestureRecognizers>
                    <Label Text="Drop table" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" FontSize="Subtitle"/>
                </Grid>
            </DataTemplate>
        </syncfusion:SfListView.FooterTemplate>
    </syncfusion:SfListView>
    <sfbusyindicator:SfBusyIndicator Grid.Row="1" AnimationType="SlicedCircle" TextColor="BlueViolet" IsBusy="{Binding IsLoading}" />
</Grid>

View sample in GitHub

Demo to update ListView after the database table is dropped in Xamarin.Forms

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied