Android Question Edittext name from variable

Claudio Parmigiani

Member
Licensed User
Longtime User
Hi all,
I have a bunch of Edittext, Labels and CheckBox:

B4X:
Sub Globals
Private R0Name As EditText
Private R0 As Label
Private R0Ena As CheckBox
Private R1Name As EditText
Private R1 As Label
Private R1Ena As CheckBox
Private R2Name As EditText
Private R2 As Label
Private R2Ena As CheckBox
End Sub
...

At a certain point of my code I would like to populate Edittexts and label with the content of some variables.
Is there some way to cycle this operation ? Something like:
B4X:
For index = 0 To Max
     "R" & index & "Name".Text = something...
     "R" & index.Text = something_else...
Next

Thank you very much for your help, as usual.
BR,
Claudio
 
Last edited:

derez

Expert
Licensed User
Longtime User
Create your views as an array : dim et(10) as edittext
Then when you want to use variable contents use et(index).text = ...
 
Upvote 0

Claudio Parmigiani

Member
Licensed User
Longtime User
Sorry for coming back again but I am still in "troubles".
In Designer I have 16 Labels and 16 Edittext.
I'd like to populate (partially or totally, it depends) Labels and EditTexts using some kind of cycle.

Label's names are set toR0, R1, R2 ... R15
Label's Event Names are set to R(0), R(1), R(2) ... R(15)
Edittext names are set to R0Name, R1Name, R2Name ... R15Name
Edittext Event Names are set to RName(0), RName (1), RName(2) ... RName(15)

First: I can not assign the desired name to Label/Edittext.
If I try to assign, for example, the name R(0) to Label 0, I get "Invalid name" error from Designer.

I declare my arrays as:

B4X:
Dim RName(16) As EditText
Dim R(16) As Label

In the middle of my code, after loading the layout, I try to populate my Labels/Edittext:
B4X:
For index = 1 To maxindex Step 1
     R(index).Text = something
     RName(index).Text = something else
Next

If I run my code I get:
java.lang.RuntimeException: Object should first be initialized (Label).
Did you forget to call Activity.LoadLayout?

How could I solve my problem ?
Thanks in advance,
BR,
C.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
try putting

B4X:
R(index).initialize("myevent")

under your FOR definition line, you just declared an empty array.
 
Upvote 0

Claudio Parmigiani

Member
Licensed User
Longtime User
Hi sorex,
no more errors, but no text displayed.

It might be because there are no R(something) Labels defined in to the Designer.
And apparently it can not be done...
Thanks anyway,
BR,
C.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you also need to add it to the activity or panel/scrollview/... so that it can be seen :)

B4X:
activity.addview(r(index),0,0,100,100)

or something like that (don't know if it's in a panel or something else)
 
Upvote 0

Picena Informatica

Active Member
Licensed User
Longtime User
B4X:
dim i as int
for i = 0 to maxindex
    dim t as edittext
    t.inizialize("yourevent")
    r(i)=t
next
 
Upvote 0

Claudio Parmigiani

Member
Licensed User
Longtime User
you also need to add it to the activity or panel/scrollview/... so that it can be seen :)

B4X:
activity.addview(r(index),0,0,100,100)

or something like that (don't know if it's in a panel or something else)


Almost there... this:
B4X:
For index = 1To maxindex
 R(index).initialize("myevent")
 Rname(index).initialize("myevent")
 Activity.addview(R(index),0,0,100%x, 100%y)
 Activity.addview(RName(index),0,0,100%x, 100%y)
 R(index).Text = something
 RName(index).Text = something else
 Next

... prints all the contents in one place.
I usually load my different layout with this Sub:

B4X:
Sub LoadLayoutToPanel (Layout As String)
 If CurrentPanel.IsInitialized Then
     Activity.RemoveViewAt(0)
 End If
   CurrentPanel.Initialize("")
   CurrentPanel.LoadLayout(Layout)
   Activity.AddView(CurrentPanel, 0, 0, 100%x, 100%y)
End Sub

How could I adjust
Activity.addview(R(index),0,0,100%x, 100%y)
Activity.addview(RName(index),0,0,100%x, 100%y)
to work with my panel ?

Thanks,
BR,
C.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
You have lost track here you shouldn't initialize a view added in design. Nor add it to the activity again. You code just puts a load of new views in the same place. Iif it says you need to initialize a view then your declaration doesn't match your designer view names.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you have an example.
B4X:
Sub Globals
    Dim lblCodeLabelsNb = 4 As Int
    Dim lblTest(6), lblCodeLabels(lblCodeLabelsNb) As Label
  
    Dim lblTest0, lblTest1, lblTest2, lblTest3, lblTest4, lblTest5 As Label
    Dim edtTest(6) As EditText
    Dim edtTest0, edtTest1, edtTest2, edtTest3, edtTest4, edtTest5 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
  
    lblTest = Array As Label(lblTest0, lblTest1, lblTest2, lblTest3, lblTest4, lblTest5)
    edtTest = Array As EditText(edtTest0, edtTest1, edtTest2, edtTest3, edtTest4, edtTest5)
  
    AddLabels
End Sub

Sub btnFillText_Click
    For i = 0 To 5
        lblTest(i).Text = "Label " & i & "Text"
        edtTest(i).Text = "EditText " & i & "Text"
    Next
  
    For i = 0 To lblCodeLabelsNb - 1
        lblCodeLabels(i).Text = "Code label " & i & "Text"
    Next
End Sub

Sub AddLabels
    Dim Left, Top, Width, Height, Space As Int
    Left = lblTest(0).Left
    Top = lblTest(0).Top + 6 * lblTest(0).Height + 20dip
    Width = 150dip
    Height = 40dip
    Space = 5dip
    For i = 0 To lblCodeLabelsNb - 1
        lblCodeLabels(i).Initialize("lblCodeLabels")
        lblCodeLabels(i).Tag = i
        lblCodeLabels(i).Color = Colors.Blue
        Activity.AddView(lblCodeLabels(i), Left, Top + i * (Height + Space), Width, Height)
    Next
End Sub

Sub lblCodeLabels_Click
    Dim lbl As Label
    Dim Index As Int
  
    lbl = Sender
    Index = lbl.Tag
    Activity.Title = "Code label " & Index & " clicked"
End Sub
You can also add views by code.

Attached a small demonstration program.
 

Attachments

  • ViewArrays.zip
    7.8 KB · Views: 166
Upvote 0

Claudio Parmigiani

Member
Licensed User
Longtime User
Klaus,
thanks for your demo, I didn't think it was so complicated...
I'll try to figure out how to adapt it for my needs.

Many thanks again to you and to all ! :)
BR,
C.
 
Upvote 0
Top