I'm working on a simple project in xamarin.forms whic i want to populate a listview from database in this format.
Item-1
Items: items from database
Locations: Location from database
Districts: Districts from database
Cheifdoms: Cheifdoms from database
UOM: UOM from database
Price: Price from database
Item-2
Items: items from database
Locations: Location from database
Districts: Districts from database
Cheifdoms: Cheifdoms from database
UOM: UOM from database
Price: Price from database
and so on
What I have tried:
For my ContentPage
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SLCommoditys.Views.CommodityListPage">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Add New"
Clicked="OnAddNewClicked" />
</ContentPage.ToolbarItems>
<ContentPage.Content>
<ListView x:Name="lvCommodity">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Spacing="2" Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Label Text="Items: " FontSize="16"/>
<Label Text="{Binding Items}" FontSize="16" TextColor="Black"/>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Location: " FontSize="16"/>
<Label Text="{Binding Location}" FontSize="16" TextColor="Black"/>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="District: " FontSize="16"/>
<Label Text="{Binding District}" FontSize="16" TextColor="Black"/>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Chiefdom: " FontSize="16"/>
<Label Text="{Binding Chiefdom}" FontSize="16" TextColor="Black"/>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Unit of Measure: " FontSize="16"/>
<Label Text="{Binding UOM}" FontSize="16" TextColor="Black"/>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Price: " FontSize="16"/>
<Label Text="{Binding Price}" FontSize="16" TextColor="Black"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
using SLCommoditys.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace SLCommoditys.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CommodityListPage : ContentPage
{
public CommodityListPage ()
{
InitializeComponent ();
}
protected override async void OnAppearing()
{
base.OnAppearing();
lvCommodity.ItemsSource = await App.SQLiteDb.GetComosAsync();
}
async void OnAddNewClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new AddNewPage
{
BindingContext = new CommodityList()
});
}
}
}
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;
namespace SLCommoditys.Models
{
public class CommodityList
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Items { get; set; }
public string Location { get; set; }
public string District { get; set; }
public string Chiefdom { get; set; }
public string UOM { get; set; }
public string Price { get; set; }
}
}
using SLCommoditys.Models;
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace SLCommoditys.Data
{
public class SQLiteHelper
{
SQLiteAsyncConnection db;
public SQLiteHelper(string dbPath)
{
db = new SQLiteAsyncConnection(dbPath);
db.CreateTableAsync<user>().Wait();
db.CreateTableAsync<commoditylist>().Wait();
}
//Insert and Update new record
public Task<int> SaveItemAsync(User person)
{
if (person.id != 0)
{
return db.UpdateAsync(person);
}
else
{
return db.InsertAsync(person);
}
}
//Delete
public Task<int> DeleteItemAsync(User user)
{
return db.DeleteAsync(user);
}
//Read All Items
public Task<list<user>> GetItemsAsync()
{
return db.Table<user>().ToListAsync();
}
public Task<int>SaveComsAsync(CommodityList como)
{
if (como.Id != 0)
{
return db.UpdateAsync(como);
}
else
{
return db.InsertAsync(como);
}
}
//Read Item
public Task<user> GetItemAsync(string Password)
{
return db.Table<user>().Where(i => i.Password == Password).FirstOrDefaultAsync();
}
public Task<list<commoditylist>> GetComosAsync()
{
return db.Table<commoditylist>().ToListAsync();
}
public Task<commoditylist> GetComoAsync(int id)
{
return db.Table<commoditylist>()
.Where(i => i.Id == id)
.FirstOrDefaultAsync();
}
}
}