Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
}
}
}
Posted
Updated 27-Mar-19 1:16am
Comments
[no name] 27-Mar-19 11:39am    
You post a bunch of code unrelated to "loading a listview" and expect what?

Find Waldo?
Mathiudi 27-Mar-19 12:00pm    
protected override async void OnAppearing()
{
base.OnAppearing();

lvCommodity.ItemsSource = await App.SQLiteDb.GetComosAsync();
}
Mathiudi 27-Mar-19 12:00pm    
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();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900