Im a bit confused, because the code I am writing does not work. I am trying to replace a view by a new view of the same type.
Here is my code:
B4X:
For Each v As View In act.GetAllViewsRecursive
If Not(v.Tag = Null) Then
vmap = v.Tag
If vmap.Get("name") = "testbutton" Then
Dim newbt as Button
newbt = CreateNewButton(vmap)
v = newbt
End If
End If
Next
Am I doing something wrong or is it just not possible to replace a view?
Only for understanding my problem:
- I do not want to remove the view and then add it again, because this changes the z-order.
- I do not want to just set v.Color because i want to change more fields that are defined within the Map called "vmap"
I don't think you can simply replace the view, you need to remove current view and add new view.
With v = newbt you are switching the pointer of object v to a new view, but you cannot replace the view in this way.. try a code like this
B4X:
dim found as boolean = false
For i = 0 to act.numberofview-1
dim v as view = act.getview(i)
If Not(v.Tag = Null) Then
vmap = v.Tag
If vmap.Get("name") = "testbutton" Then
Dim newbt as Button
newbt = CreateNewButton(vmap) 'don't know what this do, i suppose make button
act.addview(newbt,v.left,v.top,v.width,v.height)
v.removeview
found = true
else
if found then v.sendToBack
End If
End If
Next
Create a helpersub which CHANGES a Button with all what you want to change.
and then you just change the already defined button.
something like
B4X:
For Each v As View In act.GetAllViewsRecursive
if v is Button then
If Not(v.Tag = Null) Then
vmap = v.Tag
If vmap.Get("name") = "testbutton" Then
Dim buttontochange as Button = v ' get a reference to the right button
ChangeButton(buttontochange,vmap)
End If
end if
End If
Next