Android Question EditText Array

Discussion in 'Android Questions' started by Urishev, Apr 25, 2016.

  1. Urishev

    Urishev Member Licensed User

    Good day!
    How to get the data entered in the fields array EditText?
    'The user enters data
    Code:
    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: Dec 1, 2019 at 3:10 PM
  2. klaus

    klaus Expert Licensed User

    You should define an array of EditText views and use indexes from 0 to 4.
    Code:
    ' 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: Dec 1, 2019 at 3:11 PM
  3. Urishev

    Urishev Member Licensed User

    Thank you. As well, when you help!
     
  4. Urishev

    Urishev Member Licensed User

    Will another option be correct too?
    Code:
    ' in Globals
    Private EditTexts(5As 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: Dec 1, 2019 at 3:05 PM
  5. klaus

    klaus Expert Licensed 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.
     
  6. Urishev

    Urishev Member Licensed User

    Thanks, got it.
    Code:
    Sub EditTexts_EnterPressed
    log("try")
    End Sub
    Do you think this procedure will work?
     
    Last edited: Dec 1, 2019 at 4:04 PM
  7. Erel

    Erel Administrator Staff Member Licensed User

    It will work if you set the EventName parameter to "EditTexts".
     
  8. klaus

    klaus Expert Licensed 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
    Code:
    Sub EditTexts_EnterPressed
        
    Private edt As EditText
        
    Private Index As Int

        edt = 
    Sender
        Index = edt.Tag
        
    'and your code
    End Sub
     
    Erel likes this.
  9. Urishev

    Urishev Member Licensed User

    Thanks for the example, now it’s clear.
    As it turned out, a simpler variant of declaring an EditText Array also works:
    Code:
    Dim EditText(5as EditText
     
    Last edited: Dec 6, 2019 at 8:58 PM
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice