Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

XML
<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.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:

C#
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:

C#
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

C#
<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:

C#
<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>
Posted
Comments
[no name] 8-Nov-22 23:05pm    
There's no evidence that ID == i is in your table. Or even that your ToList returns anything. If it did, "i" should be in there.

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