Android Question How to get view type inside panel ?

adjie

Member
Licensed User
Below is my code :
B4X:
    For Each v As View In panel1.GetAllViewsRecursive
            v.RemoveView 'just need specific view (edittext) only to delete
        End If
    Next
So how to detect whether v is an EditText or not ?
Thanks.
 

mangojack

Well-Known Member
Licensed User
I think this a better method ...
B4X:
For i = Panel1.NumberOfViews - 1 To 0 Step -1
  Dim v As View = Panel1.GetView(i)
  If v Is EditText Then
    Panel1.Removeview
  End If
Next
 

adjie

Member
Licensed User
I think this a better method ...
B4X:
For i = Panel1.NumberOfViews - 1 To 0 Step -1
Dim v As View = Panel1.GetView(i)
If v Is EditText Then
  Panel1.Removeview
End If
Next
what is the advantage of above code compare to the first code?
 

DonManfred

Expert
Licensed User
I'll always remember it whenever doing any iteration.
If you just want to iterate through all (without deleting) the first one is perfect. But if you want to remove objects in this loop then the second solutiion is better. Both solutions i could not show better. Thanx to @mangojack for this
 

adjie

Member
Licensed User
Agree with you @DonManfred , I thing it is about programming method. Why I notice this, because of my own foolish. And this happening two me for many year in vb6 :p. So, since got reference from @mangojack, I laugh and realize that first method will come to complicated zone for some object or function.
for example :
B4X:
For n = 0 to array.upperbound(1)
    if array(n,0) = 0 then
        array.deleterow n
    end if
Next
I always do that in many place, so.. instead of iterate from the top, it is nicer to do it from the bottom. With that, it doesn't need to detect what is the current position to iterate.
Once again, thanks all.
 
Last edited:

klaus

Expert
Licensed User
There is one difference in the iteration principle between the two solutions:
With For i = Panel1.NumberOfViews - 1To0Step -1 you will get only the views on Panel1, if there is a child panel on Panel1 the views on this child panel will not be considered.

With For Each v As View In Panel1.GetAllViewsRecursive they will.
 

adjie

Member
Licensed User
There is one difference in the iteration principle between the two solutions:
With For i = Panel1.NumberOfViews - 1To0Step -1 you will get only the views on Panel1, if there is a child panel on Panel1 the views on this child panel will not be considered.

With For Each v As View In Panel1.GetAllViewsRecursive they will.
Thats make things more clear. Thanks Klaus !
 
Top