Android Question creates x labels based on the number of letters in the word

zed

Active Member
Licensed User
Hello everyone can someone help me?
I have a database that contains over 2000 words.
I take a word at random I count the number of letters and
I create as many labels as there are letters in the word.
But it does not work.
i have this error message : java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

Here is the code

b4a:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Private SQL1 As SQL
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Label1 As Label
    Private myMots As String
    
    Private pnlMots As Panel
    Private lblName() As B4XView

End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    If Not(File.Exists(File.DirInternal, "db_mots.db3")) Then
        File.Copy(File.DirAssets, "db_mots.db3", File.DirInternal, "db_mots.db3")
    End If
    SQL1.Initialize(File.DirInternal, "db_mots.db3", True)
    
    Activity.LoadLayout("Layout")
    
    
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    
    random_word
End Sub

Sub random_word
    
    pnlMots.RemoveAllViews
    
    'select a word in the database
    Dim c As Cursor
    Dim r As Int
    c = SQL1.ExecQuery("SELECT * FROM mots ORDER BY RANDOM()" )
    c.Position = 0
    r = c.RowCount
    For Row = 0 To r - 1
        c.Position = Row
        myMots = c.GetString("mots")
    Next
    c.Close
    
    Label1.Text = "view test :  " &myMots.Length &"  : "&myMots
        
    'creates (x) labels based on the number of letters in the word
    Dim dist As Int=0
    For i = 0 To myMots.Length-1
        
        lblName(i).Tag = i
        lblName(i).Text = myMots.CharAt(i)
        lblName(i).TextSize = 24
        lblName(i).TextColor = Colors.Black
        pnlMots.AddView(lblName(i), dist, 30, 40, 40)
        dist = dist + 40
    Next
    
End Sub

Thank you for your help
 

klaus

Expert
Licensed User
Longtime User
You are requesting for all words in the database randomly and use only the last one.
This could for sure be done simpler, requesting for the word in a randomly defined row.

You never create any Label.

This routine works:

B4X:
Private Sub GenerateLabels(Mot As String)
    Private dist As Int = 10dip
    Private lblName(Mot.Length) As B4XView

    pnlMots.RemoveAllViews
    For i = 0 To Mot.Length - 1
    Private lbl As Label
        lbl.Initialize("")
        lblName(i) = lbl
        lblName(i).Tag = i
        lblName(i).Text = Mot.CharAt(i)
        lblName(i).TextSize = 24
        lblName(i).TextColor = Colors.Black
        pnlMots.AddView(lblName(i), dist, 30dip, 40dip, 40dip)
        dist = dist + 40dip
    Next
End Sub

When you use dimensions for views you should use dip (density independent pixel) values and not pure pixels.
In your case the dimensions will be different depending on the screen density (dots per inch).
 
Upvote 2

Mahares

Expert
Licensed User
Longtime User
I take a word at random I count the number of letters
klaus showed you the proper way to build the labels. To use your database to extract the random word, all you need is one line of code:
B4X:
Dim Mot As String = SQL.ExecQuerySingleResult("SELECT mots FROM mots ORDER BY Random()")
Log("Le mot est: " & Mot)  'displays the random extracted word
 
Upvote 0
Top