I have a recipes app for android using Xamarin and SQLite Net as the database
In my main page if you click on the recipe title it will take you to the view page
I've been trying to query a single list from my the database but can't seem to find the correct way with the methods i've been using.
This is my view page:
<pre>="1.0"="utf-8"
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="RecipesApp.ViewRecipe">
<StackLayout Margin="20,35,20,20">
<CollectionView x:Name="collectionView">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Title}"
FontSize="Medium" />
<Label Text="{Binding Creator}"
TextColor="Silver"
FontSize="Small" />
<Label Text="{Binding Ingredients}"
TextColor="Silver"
FontSize="Small" />
<Label Text="{Binding Steps}"
TextColor="Silver"
FontSize="Small" />
<Label Text="{Binding Notes}"
TextColor="Silver"
FontSize="Small" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
and this is the c# behind it:
namespace RecipesApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ViewRecipe : ContentPage
{
public ViewRecipe()
{
InitializeComponent();
}
protected async void OnAppearing(int id)
{
base.OnAppearing();
collectionView.ItemsSource = (System.Collections.IEnumerable)await App.Database.GetItemAsync(id);
}
}
}
And this is the method GetItemAsync being called from the database:
public Task<Recipes> GetItemAsync(int id)
{
return _database.Table<Recipes>().Where(i => i.ID == id).FirstOrDefaultAsync();
}
What I have tried:
I've been able to display all lists using
<pre> public Task<List<Recipes>> GetPeopleAsync()
{
return _database.Table<Recipes>().ToListAsync();
}
but that's not my intended goal.
If it helps this is my main page as well:
<pre><?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="RecipesApp.MainPage">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Add"
Order="Primary"
Priority="0"
Clicked="OnButtonClick"/>
</ContentPage.ToolbarItems>
<StackLayout Margin="20,35,20,20">
<CollectionView x:Name="collectionView">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<StackLayout>
<Label Text="{Binding Title}"
FontSize="Medium" />
<Label Text="{Binding Creator}"
TextColor="Silver"
FontSize="Small" />
</StackLayout>
<Button Margin="-10,0,0,20" BackgroundColor="#None" WidthRequest="300" HeightRequest="50" Clicked="OpenRecipe_Button"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ContentPage>