Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private SQL1 As SQL
Private Toast As BCToast
Private GoodAnswer As Int
Private myQ, myA1, myA2, myA3 As String
Private lblQuestion As Label
Private rb1 As RadioButton
Private rb2 As RadioButton
Private rb3 As RadioButton
Private qId As Int = 1 'question id
Private nbrQ As Int 'number of questions
Private lblNbrQ As Label
Private score As Int = 0
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
B4XPages.SetTitle(Me, "Quiz")
' If the database does not exist, copy the database
If Not(File.Exists(xui.DefaultFolder, "quiz.db3")) Then
File.Copy(File.DirAssets, "quiz.db3", xui.DefaultFolder, "quiz.db3")
End If
SQL1.Initialize(xui.DefaultFolder, "quiz.db3", True)
Toast.Initialize(Root)
nbrQ = SQL1.ExecQuerySingleResult("SELECT count(*) FROM table_quiz") 'number of questions in the db
loadQuestion(qId)
End Sub
Private Sub loadQuestion(n As Int)
reset_radiobutton
lblNbrQ.Text = $"Question ${n} of ${nbrQ}"$
Dim RS As ResultSet = SQL1.ExecQuery("SELECT * FROM table_quiz where id_question ="&n&" ")
RS.NextRow
myQ = RS.GetString("question").Trim
myA1 = RS.GetString("answer1").Trim
myA2 = RS.GetString("answer2").Trim
myA3 = RS.GetString("answer3").Trim
GoodAnswer = RS.GetInt("tag")
lblQuestion.Text = myQ
rb1.Text = myA1
rb2.Text = myA2
rb3.Text = myA3
End Sub
Private Sub btCheck_Click
' check
Dim userSelected As Int
If rb1.Checked Then userSelected = 1
If rb2.Checked Then userSelected = 2
If rb3.Checked Then userSelected = 3
If userSelected = GoodAnswer Then 'compare the answers
score = score+1
Toast.Show($"You win [b]Score : ${score} points[/b]"$)
Else
Toast.Show($"You lost : [b]Score : ${score} points[/b]"$)
End If
qId = qId+1 'question id
If qId <= nbrQ Then
loadQuestion(qId) 'next question
Else
Sleep(1000)
Toast.Show($"Completed Quiz. You have a score of [b]${score} points[/b]"$)
End If
End Sub
Private Sub reset_radiobutton
rb1.Checked = False
rb2.Checked = False
rb3.Checked = False
End Sub