Android Question moving records in json

jchal

Active Member
Licensed User
Longtime User
hi all
i have this code and i would like to select a random question from the list and then press the button (Bnext_Click) to move to the next random question but it has to remeber the questions that had passed
my code is below
how do i do the random selection and how do i make it remember wich question it passed?
B4X:
Sub LoadQuizQuestion
    Dim jobadd As HttpJob
 
    jobadd.Initialize("quizquestion", Me)
    jobadd.Download("http://www.mydomain.com/quizq.php")
    ProgressDialogShow("Downloading")
End Sub

Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success = True Then
        Dim strReturn As String = Job.GetString
        Dim parser As JSONParser
        parser.Initialize(strReturn)
        If Job.JobName = "quizquestion" Then
            Dim quizlist As List
       
            quizlist = parser.NextArray 'returns a list with maps
           
            For i = 0 To quizlist.Size - 1
                Dim m As Map
                m = quizlist.Get(i)
                Dim TL As quizpar 'TwoLines
               
                TL.First = m.Get("quizquestion")
                TL.Second = m.Get("quizanswer1")
                TL.third=m.Get ("quizanswer2")
                TL.forth=m.Get ("quizanswer3")
                TL.five=m.Get("quizanswer4")
                TL.six=m.Get("quizanswer5")
               
                Label1.Text= TL.First
                Button1.Text=TL.Second
                Button2.Text=TL.third
                Button3.Text=TL.forth
                Button4.Text=TL.five
                LcorectAsw.text=TL.six
               
                Exit
               
            Next
        Else If Job.JobName = "LogOut" Then
            Dim act As String = parser.NextValue
            If act = "LoggedOut" Then
                ToastMessageShow("Logout successful", True)
                StartActivity(Main)
                Activity.Finish
            End If
        Else
            ToastMessageShow("Error: Invalid Value", True)
        End If
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

Sub Bnext_Click
     LoadQuizQuestion
End Sub
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
There are many possible answers, which one is best for you really depends on your coding skill level and how much effort you want to put into it.

You could go to the extent of creating a class that handles everything from the HTTP query to serving up the questions and just returns what you need, or you could just make your local "quizlist" variable a global variable and work with it that way.

If it were my app, I would probably make a global variable containing a LIST of a user-defined TYPE containing the questions data I needed (which the JobDone would load up initially) and then use the next button event to grab a random entry from that list and display it (removing it from the list so next time I wouldn't display it again).

Again, there are several ways to implement it, this is just the first one that came to mind.
 
Upvote 0

jchal

Active Member
Licensed User
Longtime User
If it were my app, I would probably make a global variable containing a LIST of a user-defined TYPE containing the questions data I needed (which the JobDone would load up initially) and then use the next button event to grab a random entry from that list and display it (removing it from the list so next time I wouldn't display it again).
how can you do it? lets say Dim L As List in globals
then? and how will you do the rnd? for the random selection of the question?
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Here is a brief pseudo-code outline (disclaimer: not tested, no error checking, etc.):

B4X:
Sub Globals
    Type QuestionData(Number As Int, Question As String, Answer As String, PointsValue As Int)
    Private MyQuestions As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
    MyQuestions.Initialize
 
    Dim poTest As QuestionData  'do this every time or you will be using the same QuestionData variable
    poTest.Number = 1
    poTest.Question = "Question 1"
    poTest.Answer = "Answer 1"
    poTest.PointsValue = 5
    MyQuestions.Add(poTest)
 
    Dim poTest As QuestionData
    poTest.Number = 2
    poTest.Question = "Question 2"
    poTest.Answer = "Answer 2"
    poTest.PointsValue = 10
    MyQuestions.Add(poTest)
 
    If FirstTime Then
        RndSeed(DateTime.Now) 
    End If
End Sub

Private Sub bntNext_Click
    Dim poData As QuestionData
    Dim piQuest As Int
 
    piQuest = Rnd(0, MyQuestions.Size)
    poData = MyQuestions.Get(piQuest)
    MyQuestions.RemoveAt(piQuest)
 
    ' here you would call your routine to ask the question (passing the poData variable)
End Sub

If this were for a "real"/"production" app, then I would probably encapsulate all of the question-handling in its own class in order to make it easier to maintain/unit-test.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
This doesn't seem like an ideal use of JSON to me...

I would import the information in to a database, then use the information, once imported, in the database.
 
Upvote 0

jchal

Active Member
Licensed User
Longtime User
this is what i am doing eps but i use json, as i would like to have the questions in remote database
 
Upvote 0
Top