Android Question Reset all B4XSwitch switches in a list

nwhitfield

Active Member
Licensed User
Longtime User
In my iOS app, which I'm porting to Android/B4X, I have a custom list view, containing panels of filter options. Each one has a text view and a switch. I can easily reset all filters to the default with this code:

B4X:
 For i = 0 To userFilter.GetSize -1
   Dim p As Panel = userFilter.GetPanel(i)
   Dim s As Switch = p.GetView(0)
   s.value = false

Unfortunately, there doesn't seem to be an easy way to do the same with B4XSwitch. How can I reset the values (and, for filtering, check which ones have been set to which value)?

One alternative for the latter would be to have an array of values, updating when a switch is changed, but that still doesn't solve the reset issues; I could, I guess, just rebuild all the panels, but that seems a bit extravagant.
 

nwhitfield

Active Member
Licensed User
Longtime User
That gives an error during compilation:

error: incompatible types: View cannot be converted to b4xswitch

I've also tried with this:

B4X:
   Dim v As B4XView = p.GetView(0)
   Dim s As B4XSwitch = v
   s.Value = False

But that results in a run-time exception:

java.lang.RuntimeException: Field: ba not found in: anywheresoftware.b4a.BALayout
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
B4XPanel? Unless something's gone seriously weird with my libraries, that's an unknown type. (XUI 1.90, XUI Views 2.13)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4XPanel? Unless something's gone seriously weird with my libraries, that's an unknown type.
i guess it is a misspelling. I think he meant B4XView.
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
Same runtime exception, field 'ba' not found, as above.

And if I add an extra level of indirection
B4X:
 dim tmp as b4xview = p.getView(0)
dim s as b4xswitch = tmp

then I get the exception

java.lang.ClassCastException: anywheresoftware.b4a.BALayout cannot be cast to com.bluf.navigator.b4xswitch

I shall sleep on this; and maybe think about using checkboxes in the morning.
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
I've tried that, too - they did in fact get swapped round in the copy and paste from B4i to B4A. But that doesn't appear to be the problem, either.

If I'm dealing with a switch as the sender of an event, as I do elsewhere, that works just fine. But it seems to me that when I try to retrieve it from a parent view, it's as if it's disassembling into the component parts that make it up.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
dim tmp as b4xview = p.getView(0)
dim s as b4xswitch = tmp
i did not used b4xswitch.
But i remember that customviews reference itself using the Tag.

worth a try

B4X:
dim tmp as b4xview = p.getView(0)
dim s as b4xswitch = tmp.Tag
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
I've tried that, too - they did in fact get swapped round in the copy and paste from B4i to B4A. But that doesn't appear to be the problem, either.

If I'm dealing with a switch as the sender of an event, as I do elsewhere, that works just fine. But it seems to me that when I try to retrieve it from a parent view, it's as if it's disassembling into the component parts that make it up.

To check the index of b4xswitch run a foreach loop and print the type of all child views.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
i did not used b4xswitch.
But i remember that customviews reference itself using the Tag.
That's the correct answer.
Note that it is a convention used in XUI Views. It is not something that is set by default to all custom views.

You can write it like this:
B4X:
Dim s As B4XSwich = pnl.GetView(0).Tag
 
Upvote 0
Top