Assigning values to variables and adding to list.

skcud

Member
Hey Guys, I'm working in collab to develop an app and have hit a brick wall, was hoping someone here could help as I'm so new. This post is quite long, so thank you in advance for your time. I'd really really appreciate some help.
So I have an array of variables called YSpeed(y) and y = 1 to i. The user defines 'i' upon starting the app. I have a page titled 'J' with an edit text. When the 'Next' button is hit, 'J' is increased by 1 until it equals the user defined 'i' at which point to app moves to the next page. The user enters a number into the edittext that they wish to assign to a YSpeed variable. I use YSpeed(J) = edittext1.text. As J always equals the page number, this means each page assigns a number to a different YSpeed. After making the YSpeed equal to the edittext, the button then writes YSpeed(J) to a list so that, once the user has finished using the form, there will be a list of YSpeed(1) to YSpeed(I) with all their values.

This has given me many different errors, most of which I overcome with trial and error and most of my solutions lead to more problems.I am very new to programming on the whole so would appreciate if you could speak in basic terms (pun intended ;D ). Some of my coding seems quite superfluous because I have added extra buttons to run tests and there are more variables that I haven't had chance to even use yet. I also get the feeling that I'm working with completely inefficient methods. If you think so, please let me know. The only reason for this is because I'm new.

This is the coding for the page with the edittext.

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim label1 As Label
   Dim J As Double
   Dim textwriter1 As TextWriter
   Dim list1 As List
   Dim Button1 As Button
   Dim Button2 As Button
   Dim EditText1 As EditText
   Dim EditText2 As EditText
   Dim EditText3 As EditText   
   For y = 0 To main.i
      Dim YSpeed(y) As Double
      Next
   Dim Button3 As Button
End Sub



Sub Activity_Create(FirstTime As Boolean)
activity.LoadLayout("details")
j = 1
label1.Initialize("")
activity.AddView(Label1, 5%x, 10%y, 90%x, 10%y)
Label1.text = "Bend " & j & " Options"
End Sub


Sub Button1_Click      
YSpeed(j) = edittext1.Text
    List1.Initialize
    List1.Add(YSpeed(j))

    File.WriteList(File.DirInternal, "Values", List1)


If j = main.i Then
   StartActivity("Summary")
   j=1
Else
   j = j + 1
   Label1.text = "Bend " & j & " Options"

End If
End Sub

When I run the app, once I have pressed the button enough times for 'J' to equal 'I', the app pauses and the line
B4X:
YSpeed(j) = edittext1.Text
is highlighted yellow.
 

klaus

Expert
Licensed User
Longtime User
Without knowing exactly what you want to do, and with only a code fragment it's very difficult to give you a concrete advice. But I suggest you the code below:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Label1 As Label
    Dim j As Double
    Dim TextWriter1 As TextWriter
    Dim Button1 As Button
    Dim Button2 As Button
    Dim EditText1 As EditText
    Dim EditText2 As EditText
    Dim EditText3 As EditText    
    Dim Button3 As Button
    Dim List1 As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
    List1.Initialize
    End If
    Activity.LoadLayout("details")
    j = 1
    Label1.Initialize("")
    Activity.AddView(Label1, 5%x, 10%y, 90%x, 10%y)
    Label1.text = "Bend " & j & " Options"
End Sub

Sub Button1_Click        
    List1.Add(EditText1.Text)

    If j = main.i Then
        File.WriteList(File.DirInternal, "Values", List1)
        j = 1
        StartActivity("Summary")
    Else
        j = j + 1
        Label1.text = "Bend " & j & " Options"
    End If
End Sub
Best regards.
 
Upvote 0

skcud

Member
Thanks for your help! I actually managed to figure it out after I'd shut everything down last night. It looks like you altered my code to simplify writing information to the list, but what I had was essential. The thing that killed it was that I was initialising the list every time I pressed the button. It looks like you actually picked up on that. Thanks again :)
 
Upvote 0
Top