Home > database >  Cannot list contents of db file in Xamarin
Cannot list contents of db file in Xamarin

Time:02-13

I want to create an easy quiz application that gets values from a .db file.

In my MainPage.xaml.cs I want to list all my questions (with just one answer option for testing, from a database that is embedded and is implemented correctly for both platforms) in a list form just for now, but I get a white empty stuff without any error msg.

DataModels/Questions.cs

namespace quiztest.DataModels
{ 
    public class Questions
    {
        [PrimaryKey]
        public int ID { get; set; }
        public string Field{ get; set; }
        public string Question{ get; set; }
        public string Answer1 { get; set; }
        public string Answer2 { get; set; }
        public string Answer3 { get; set; }
        public string Answer4 { get; set; }
        public string CorrectAnswer { get; set; }
    }
}

MainPage.xaml.cs

using quiztest.DataModels;

...
    
protected override void OnAppearing()
            {
                base.OnAppearing();
                using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
                {
                    var questions = conn.Table<Question>().ToList();
                    quizListView.ItemsSource = questions;
    
    
                }
    
            }

MainPage.Xaml

        <StackLayout>
        <Label Text="Contents:"/>
        <Label x:Name="labelrandom"/>
        <ScrollView>
            <ListView x:Name="quizListView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding Question}" Detail="{Binding Answer1}" TextColor="Aquamarine"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ScrollView>
    </StackLayout>

I feel that the issue is related to getting the correct columns to show up on the textcell but I am just started learning and I am not an expert.

CodePudding user response:

Replace

var questions = conn.Table<Question>().ToList();
quizListView.ItemsSource = questions;

with

 var questions = (from x in conn.Table<Question>() select x).ToList();  
 quizListView.ItemsSource = questions ;  

CodePudding user response:

I advice you to use collectionview instead. to fix your problem bind the listview to a observablecollention

 private ObservableCollection<Questions> questionsList;
        public ObservableCollection<Questions> QuestionsList
        {
            get { return questionsList; }
            set
            {
                if (questionsList != value)
                {
                    questionsList = value;
                  
                }
            }
        }

then in your Function :

protected override void OnAppearing()
            {
                base.OnAppearing();
                using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
                {
                     var quesList = (from x in conn.Table<Question>() select x).ToList();  
                    QuestionsList = new ObservableCollection<Questions>(quesList);
                    quizListView.ItemsSource = QuestionsList;
    
    
                }
    
            }

or you can bind it Directly to the listview

<ListView x:Name="quizListView" ItemsSource={Binding QuestionsList}>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding Question}" Detail="{Binding Answer1}" TextColor="Aquamarine"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
  • Related