I've found it's handy sometimes when you can access a view by its index if it's known. You don't have to use the GetAllViewsRecursive method and check the type (EditText is a special kind of Label, by the way) So I've done some tests and I've found the following
1. The index is based on the order it's added not the physical location it's placed.
2. Any view added to become the sibling of some existing XUI views (such as B4XPlusMinus and B4XFloatTextField) can not be accessed by index
Please guide me if my observations are incorrect.
1. The index is based on the order it's added not the physical location it's placed.
2. Any view added to become the sibling of some existing XUI views (such as B4XPlusMinus and B4XFloatTextField) can not be accessed by index
Please guide me if my observations are incorrect.
B4X:
Dim label1Index As Int=-1
Dim Label2Index As Int=-1
Dim i As Int=0
For Each v As B4XView In Activity.GetAllViewsRecursive
Log(i & ", " & v.Tag)
If v.Tag Is B4XPlusMinus Then
Dim bpm As B4XPlusMinus=v.Tag
LogColor("selected value=" & bpm.SelectedValue, Colors.Red)
LogColor("bpm.tag=" & bpm.tag, Colors.Red)
Else If v.Tag Is B4XFloatTextField Then
Dim fedt As B4XFloatTextField=v.Tag
LogColor("selected text=" & fedt.Text, Colors.Red)
LogColor("tag=" & fedt.tag, Colors.Red)
else If v Is Button Then
Dim btn As Button=v
LogColor("Button text=" & btn.Text, Colors.Red)
LogColor("button tag=" & btn.tag, Colors.Red)
Else if v Is EditText Then
Dim edt As EditText=v
LogColor("edittext: text=" & edt.Text & ", tag=" & edt.Tag, Colors.red)
Else if v Is Spinner Then
Dim spn As Spinner=v
LogColor(" spinner1 selected item=" & spn.SelectedItem & ", tag=" & spn.Tag, Colors.red)
Else if v Is Label Then
'label
Dim lbl As Label=v
If lbl =Null Or lbl.Tag=Null Then Continue
Select lbl.Tag
Case "Label1"
LogColor("lbl1: text=" & lbl.Text & ", tag=" & lbl.Tag, Colors.red)
label1Index=i
Case "Label2"
LogColor("lbl2: text=" & lbl.Text & ", tag=" & lbl.Tag, Colors.red)
Label2Index=i
End Select
End If
i = i +1
Next
If label1Index>=0 Then
Dim lbl As Label=Activity.GetView(label1Index)
LogColor("label1 text=" & lbl.Text, Colors.Red)
LogColor("label1 tag=" & lbl.tag, Colors.Red)
End If
If Label2Index>=0 Then
'Label2 is the very last view added
lbl=Activity.GetView(Label2Index) '<==this line causes error
LogColor("label2 text=" & lbl.Text, Colors.Red)
LogColor("label2 tag=" & lbl.tag, Colors.Red)
End If
End Sub
9, Label2
lbl2: text=Label2 text, tag=Label2
10, Spinner1
spinner1 selected item=Tuesday, tag=Spinner1
11, null
label1 text=Label1 text
label1 tag=Label1
Error occurred on line: 101 (Main)
java.lang.RuntimeException: Object should first be initialized (View).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6306)
at android.widget.TextView.performClick(TextView.java:11201)
at android.view.View$PerformClick.run(View.java:23962)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
lbl2: text=Label2 text, tag=Label2
10, Spinner1
spinner1 selected item=Tuesday, tag=Spinner1
11, null
label1 text=Label1 text
label1 tag=Label1
Error occurred on line: 101 (Main)
java.lang.RuntimeException: Object should first be initialized (View).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6306)
at android.widget.TextView.performClick(TextView.java:11201)
at android.view.View$PerformClick.run(View.java:23962)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)