Android Question [Solved] How to loop through a series of objects?

Mark Read

Well-Known Member
Licensed User
Longtime User
I have a sub which creates a GUI:

B4X:
Sub Build_GUI2
    'Prepare gradient
    cols1(0) = Colors.ARGB(255,167,221,241)
    cols1(1) = Colors.White
    gd1.Initialize( "BOTTOM_TOP",cols1)
    gd2.Initialize( "TOP_BOTTOM",cols1)
    gd1.CornerRadius=0
    gd2.CornerRadius=0
    Activity.Background=gd2
   
    Dim Labellist As List
    Labellist.Initialize
    Labellist.Add("Auto:")
    Labellist.Add("Autocolor:")
    Labellist.Add("Kennzeichen:")
    Labellist.Add("Mitgliedsnummer:")
    Labellist.Add("Name:")
    Labellist.Add("Handynummer:")
   
    Labellist.Add("VW Passat")
    Labellist.Add("Hell Blau")
    Labellist.Add("QB 145BG")
    Labellist.Add("123456789")
    Labellist.Add("John Doe")
    Labellist.Add("06215589589")
   
   
    For i=0 To 5
        Dim lbl As Label
        Dim edttxt As EditText
       
        edttxt.Initialize("edttxt_Edit")    'All edittexts use the same sub, tag determines value
        lbl.Initialize("")
       
        'Add label
        Activity.AddView(lbl, 15dip, i*100dip+10dip, Activity.Width/2, 40dip)
        lbl.Background=gd1
        lbl.Text=Labellist.Get(i)
        lbl.TextColor=Colors.Black
       
        'Add Edittext
        Activity.AddView(edttxt, 25dip, i*100dip+50dip, Activity.Width/2, 40dip)
        edttxt.Background=gd1
        edttxt.Tag=Labellist.Get(i)
        If SetDummyValues Then
            edttxt.Text=Labellist.Get(i+6)
        End If
       
    Next

    'Buttons
    Dim bmd As BitmapDrawable
    bmd.Initialize(LoadBitmap(File.DirAssets, "close.png"))
    btnBack.Initialize("btnBack")
    btnBack.Background = bmd
    Activity.AddView(btnBack, 100%x-50dip, 100%y-50dip, 48dip,48dip)
   

End Sub

Each Edittext can be changed. When finished, I need to get the new values of each edittext to save a settings file. This is what I have tried:

B4X:
Sub edttxt_Edit_EnterPressed

    For Each edttxt_Edit As EditText In Activity.GetAllViewsRecursive
        Log(edttxt_Edit.Tag)
        Log(edttxt_Edit.Text)
    Next   

End Sub

Which gives me this in the log (Rapid debugger and emulator):

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (settings) Create, isFirst = true **
** Activity (settings) Resume **
null
Auto:
Auto:
VW Passat
null
Autocolor:
Autocolor:
Hell Blau
null
Kennzeichen:
Kennzeichen:
QB 145BG
null
Mitgliedsnummer:
Mitgliedsnummer:
123456789
null
Name:
Name:
John Doe
null
Handynummer:
Handynummer:
06215589589
null

The problem is the NULL and the edittext which is changed, deletes the text. I cannot see my mistake!
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Update: The problem with deletion I have solved. The sub name had an underline (edttxt_Edit), which I changed to edtxtEdit and now the value stays.

Changed the sub to:

B4X:
Sub edttxtEdit_EnterPressed
    Dim Settings As Map
    Settings.Initialize
    For Each edttxt_Edit As EditText In Activity.GetAllViewsRecursive
       
        Settings.Put(edttxt_Edit.Tag, edttxt_Edit.Text)
    Next   
    Log(Settings)
End Sub

which gives the log:

(MyMap) {null=, Auto:=VW Passat, Autocolor:=Hell Blau, Kennzeichen:=QB 145BG, Mitgliedsnummer:=123456789, Name:=John Doe, Handynummer:=06215589589}

Why the "null=" ? What am I missing?
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Maybe there are other views than edittext too in this recursive call???
As from your code there is a label too....
this may cause your null

- Get all VIEWs
- Check if the view is a edittext
- If it is a EditText cast the VIEW to a EditText
- and then use the tag from it...

Something like

B4X:
    For Each v As View In Activity.GetAllViewsRecursive
    if v is EditText then
        dim edt as edittext = v 
        Settings.Put(edt.Tag, edt.Text)
        end if
    Next
 
Last edited:
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Thx Manfred, you are correct, there are six labels and six edittexts (Label, edittext, label, edittext....).

The Null is gone. Many thanks.
 
Upvote 0
Top