Android Question randon values in a fill list

Edu Chamon

Member
Licensed User
Hi guys good morning (here in Brazil).
Can you help me?
I am asking some questions to help my daughter in school.
use sqlite.
rowid 1 question 1
rowid 2 question 2
rowid 3 question 3
rowid 4 question 4
if i read with order by desc i will have 4,3,2,1
I throw these values in a list.
I would like to have these same values in the list, but at random, would anyone have any ideas please?
1st. time 4.2.1.3
2nd instead 1,2,3,4
and this interval increases as I ask more questions to avoid being monotone
if I use rnd, sometimes it will not cover all values
tks
regards
 

MarkusR

Well-Known Member
Licensed User
example , see also RndSeed
B4X:
Sub Process_Globals  
    Type MyData(no As Int,rowid As Int,text As String)

Sub Test
   
    Dim l As List
    l.Initialize

    Dim q As MyData
    q.no = Rnd(0,100)
    q.text = "Hello"
    l.Add(q)

    Dim q As MyData
    q.no = Rnd(0,100)
    q.text = "World"
    l.Add(q)
   
    l.SortType("no",True)

    For Each q As MyData In l
        Log(q.text)
    Next
   
End Sub
 

emexes

Well-Known Member
Licensed User
Assuming that the number of questions is feasible to store in a list eg < 10000, then another approach is:
B4X:
Dim QuestionList As List
QuestionList.Initialize

QuestionList.AddAll( SQL_GET_ALL QuestionIds )

Do While student still awake
    QuestionAt = Rnd(0, QuestionList.Size)
    QuestionId = QuestionList.Get( QuestionAt )
 
    Question = SQL_GET( QuestionId )
    Ask Question

    If answer correct Then
        QuestionList.RemoveAt( QuestionAt )    'remove if correct, or leave in list if incorrect, to be asked again later
    End If
Loop
which has the nice feature of re-asking questions until they are correctly answered... but not repeatedly over-and-over, just randomly in the future.
 
Last edited:

Edu Chamon

Member
Licensed User
Very cool, I learned a lot from your post, thank you very much.

please, how could i return sql within the list?

B4X:
dim s as sql
...
...
questionlist.addAll( s.select rowid from questions order by random())
 

MarkusR

Well-Known Member
Licensed User
the select statement is here in a string.

https://www.b4x.com/android/help/sql.html
example from link
B4X:
Dim Cursor As Cursor
Cursor = SQL1.ExecQuery("SELECT col1, col2 FROM table1")
For i = 0 To Cursor.RowCount - 1
  Cursor.Position = i
  Log(Cursor.GetString("col1"))
  Log(Cursor.GetInt("col2"))
Next
Cursor.Close
you can work with the data in cursor direct or you can copy it into a struct/type and put this into a list.
 
Top