Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/syncfusionexamples/loading-data-from-sqlite-online-database-from-xamarin.forms-listview-

This repository contains the sample which loading data from SQlite online database in Xamarin.Forms ListView
https://github.com/syncfusionexamples/loading-data-from-sqlite-online-database-from-xamarin.forms-listview-

database listview sqlite xamarin xamarin-forms

Last synced: 27 days ago
JSON representation

This repository contains the sample which loading data from SQlite online database in Xamarin.Forms ListView

Awesome Lists containing this project

README

        

# Loading data from SQLite online database

The SfListView allows binding the data from online database with the help of `Azure App Service`. To perform this, follow the steps:

Step 1: Get URL to store the data online.
Step 2: Create table using AppServiceHelpers.
Step 3: Populate the data into the table using the ObservableCollection Items.
Step 4: Bind it to SfListView using SfListView.ItemSource property.
Step 5: Refer to the following link to know more about working with Azure App Service.
[https://blog.xamarin.com/add-a-backend-to-your-app-in-10-minutes/](https://blog.xamarin.com/add-a-backend-to-your-app-in-10-minutes/)

`Note`

* Microsoft.Azure.Mobile.Client(v.2.1.1)
* Microsoft.Azure.Mobile.Client.SQLiteStore(v.2.1.1)
* AppService.Helpers (Does not support UWP platform)
* AppService.Helpers.Forms (Does not support UWP platform)

Refer to the following code which illustrates, how to initialize the library with the URL of the Azure Mobile App and registering the Model with the client to create a table.

```
public App()
{
InitializeComponent();
IEasyMobileServiceClient client = new EasyMobileServiceClient();
client.Initialize("http://xamarin-todo-sample.azurewebsites.net");
client.RegisterTable();
client.FinalizeSchema();
MainPage = new NavigationPage(new Pages.ToDoListPage(client));
}
```
Refer to the following code which illustrates, how to create a table using AppServiceHelpers and insert items in it.

```
using AppServiceHelpers.Abstractions;
using AppServiceHelpers.Models;
public class BaseAzureViewModel : INotifyPropertyChanged where T : EntityData
{
IEasyMobileServiceClient client;
ITableDataStore table;

public BaseAzureViewModel(IEasyMobileServiceClient client)
{
this.client = client;
table = client.Table();
}

// Returns an ObservableCollection of all the items in the table
ObservableCollection items = new ObservableCollection();
public ObservableCollection Items
{
get { return items; }
set
{
items = value;
OnPropertyChanged("items");
}
}

// Adds an item to the table.
public async Task AddItemAsync(T item)
{
await table.AddAsync(item);
}

// Deletes an item from the table.
public async Task DeleteItemAsync(T item)
{
await table.DeleteAsync(item);
}

// Updates an item in the table.
public async Task UpdateItemAsync(T item)
{
await table.UpdateAsync(item);
}

// Refresh the table and synchronize data with Azure.
Command refreshCommand;
public Command RefreshCommand
{
get { return refreshCommand ?? (refreshCommand = new Command(async () => await ExecuteRefreshCommand())); }
}

async Task ExecuteRefreshCommand()
{
if (IsBusy)
return;

IsBusy = true;

try
{
var _items = await table.GetItemsAsync();
Items.Clear();
foreach (var item in _items)
{
Items.Add(item);
}
IsBusy = false;
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
}
}

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged == null)
return;

PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
```
Refer to the following code which illustrates, how to bind the table contents into the SfListView.

```


















```

```
public partial class ToDoListPage : ContentPage
{
public ToDoListPage(IEasyMobileServiceClient client)
{
InitializeComponent();
var viewModel = new ViewModels.ToDosViewModel(client);
BindingContext = viewModel;
listView.ItemsSource = viewModel.Items;
}

private void FetchButton_Clicked(object sender, EventArgs e)
{
var viewModel1 = (ToDosViewModel)BindingContext;
viewModel1.RefreshCommand.Execute(null);
}
}
```
To know more about Xamarin.Forms ListView, please refer our documentation [here](https://help.syncfusion.com/xamarin/sflistview/working-with-sflistview)