Android Question Errors when using ForEach... Next on Views

markm

Member
Licensed User
Longtime User
Hi, so far, really liking B4A and find it very familiar and easy to use. However, I'm running into an issue I've searched for and have not found an answer for.
I get the error "java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText" when trying to run the following code:
B4X:
For Each et as EditText in Activity
       et.text = ""
Next

or

B4X:
For Each et as EditText in Activity
       If isnumber(et.text) then
             <code>
       End If
Next

I wrapped with an if/then which makes it run correctly other times, but at least 50% of the time, it still gives the above error:
B4X:
For Each et as EditText in Activity
        If et is EditText then
                  et.text = ""
        End If
Next
I only need to test against EditText views, and if I try:
B4X:
For Each et as View in Activity
       If et is EditText Then
                et.text = ""
       End If
Next
then I get an error "Unknown Member: Text"

For now, there are only 8 EditText views on the activity, so I just set up a sub to set them to blank, but I have a couple of Activities lined up that will have several EditText views and I'll need an easy way to batch work with the text in them.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Give this a try...
B4X:
    For Each v As View In Activity.GetAllViewsRecursive
        If v Is EditText Then
            Dim et As EditText = v
            et.Text = ""
        End If
Next
 
Upvote 0
Top