Android Question EditText Array

Urishev

Member
Licensed User
Longtime User
Good day!
How to get the data entered in the fields array EditText?
'The user enters data
B4X:
Sub EnterNumer
  Dim EditText1 As EditText
    For i = 1 To 5
         EditText1.Initialize ("")
         Numer(i)= EditText1.Text   
    Next
End Sub
'Read the entered data
Sub ReadNumer
  Dim EditText1 As EditText
    For i = 1 To 5
         EditText1.Initialize ("")
         EditText1.Text  = Numer(i)
    Next
End Sub

What is the error?
 
Last edited:

klaus

Expert
Licensed User
Longtime User
You should define an array of EditText views and use indexes from 0 to 4.
B4X:
' in Globals
Private EditTexts() As EditText

' in Activity_Create
EditTexts = Array As EditText(EditText0, EditText1, EditText2, EditText3, EditText4)

Sub EnterNumer
    For i = 0 To 4
        Numer(i)= EditTexts(i).Text
    Next
End Sub
'Read the entered data
Sub ReadNumer
    For 0 = 1 To 4
        EditTexts(i).Text = Numer(i)
    Next
End Sub
 
Last edited:
Upvote 0

Urishev

Member
Licensed User
Longtime User
Will another option be correct too?
B4X:
' in Globals
Private EditTexts(5) As EditText
'further as it was:
Sub EnterNumer
    For i = 0 To 4
        Numer(i)= EditTexts(i).Text
    Next
End Sub
'Read the entered data
Sub ReadNumer
    For 0 = 1 To 4
        EditTexts(i) = Numer(i)
    Next
End Sub
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
No, you need to define which EditText object belongs to each item in the array.
That's what EditTexts = Array As EditText(EditText0, EditText1, EditText2, EditText3, EditText4) does.
You cannot define an array of objects in the Designer.

I just saw that EditTexts(i) = Numer(i) should be EditTexts(i).Text = Numer(i), amended in post #2.
 
Upvote 0

Urishev

Member
Licensed User
Longtime User
Thanks, got it.
B4X:
Sub EditTexts_EnterPressed
log("try")
End Sub
Do you think this procedure will work?
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
To check which EditText raised the event you should use the Sender object and the Tag property:
You could set the Tag property to 0 for EditText0, to 1 for EditText1 etc. to get the Index
B4X:
Sub EditTexts_EnterPressed
    Private edt As EditText
    Private Index As Int

    edt = Sender
    Index = edt.Tag
    'and your code
End Sub
 
Upvote 0

Urishev

Member
Licensed User
Longtime User
Thanks for the example, now it’s clear.
As it turned out, a simpler variant of declaring an EditText Array also works:
B4X:
Dim EditText(5) as EditText
 
Last edited:
Upvote 0
Top