Wish CustomListView with SWITCH

westingenieria

Active Member
Licensed User
Longtime User
I've managed to put switch in CustomListView but I fail to capture what is that changes its state (checked or not). Sender is switchwrapper and not view.

thanks.
 

qsrtech

Active Member
Licensed User
Longtime User
"westingenieria, post: 216201, member: 33595"]I've managed to put switch in CustomListView but I fail to capture what is that changes its state (checked or not). Sender is switchwrapper and not view.

thanks.
Can you pls share how you added to list view in code cause when I try I get "view" error
 

westingenieria

Active Member
Licensed User
Longtime User
B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    p.Color = Colors.ARGB(180,0,0,0)
    Dim chk As Switch
    chk.Initialize("chk",0,True)
    chk.Gravity = Gravity.CENTER_VERTICAL
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.LEFT)
    lbl.Text = Text
    lbl.TextSize = 18
    lbl.TextColor = Colors.White
    'b.Text = "Click"
    p.AddView(lbl, 5dip, 2dip, 80%x, Height - 4dip) 'view #0
    'p.AddView(chk, 80%x, 2dip, 20%x, Height - 4dip) 'view #1
'    p.Left = Width
    chk.AddToParent(p,280dip,2dip,100dip,Height - 4dip)

    Return p
End Sub

...
...
...

B4X:
Sub chk_CheckedChange(Checked As Boolean)
    Log(Sender)

    Dim index As Int
    index = clvRutasO.GetItemFromview(Sender) 'HERE ERROR TIPE SWITCHWRAPPER <> VIEW
    Dim pnl As Panel
    pnl = clvRutasO.GetPanel(index)
    Dim chk As Switch
    chk = pnl.GetView(1)
    If chk.Checked = True Then
        'agrega a la lista
        lstRutas.Add(clvRutasO.GetValue(index))
    Else
        'quita de la lista
        lstRutas.RemoveAt(lstRutas.IndexOf(clvRutasO.GetValue(index)))
    End If
    'Msgbox("Item value: " & clvRutasO.GetValue(index) & CRLF & "Check value: " & chk.Checked, "")
End Sub

HELP PLEASE !!!! THANKS
 

qsrtech

Active Member
Licensed User
Longtime User
I'm close to a solution, do you have any idea what the two initialize values mean when initializing a switch?
 

qsrtech

Active Member
Licensed User
Longtime User
[QUOTE Dim chk As Switch
chk.Initialize("chk",0,True)
[/QUOTE]

what does "0" and "true" do?
 

qsrtech

Active Member
Licensed User
Longtime User
WORK AROUND:
I have an updated Switch Library. Not too good with Java(yet) but at least this will help us move forward.
Now the initialize args can be used as an ID(int) and initial checked value (boolean)

Might seem a bit of a hack but here's how to use it now. When you create a new switch in code do something like this:
B4X:
SwitchID=SwitchID+1            
ASwitch.Initialize("HandleBoolean",SwitchID,Property.DefaultValue.ToLowerCase="true")
ASwitch.AddToParent(p,AWidth-AHeight*2,ATop,AHeight*2,AHeight)
Dim v As View
v=ASwitch.switchView 'this gives us the actual view
v.Tag=ASwitch 'this will give us our switch object in the checked event

Here's how to use it in the event (note it has a new signature):
B4X:
Sub HandleBoolean_CheckedChange (Value As Boolean,TheSwitch As Object)
Dim r As Reflector
Dim p as view
Dim Aview As View
Dim VSwitch As Switch'this gives us back our switch object
Aview=TheSwitch'gives us a view object to get the Tag
VSwitch=Aview.Tag
r.Target = AView
p=r.RunMethod("getParent") 'this gives you the switches parent
'now you can do pretty much anything you want

There you go...
 

Attachments

  • QSRSwitch.zip
    3.5 KB · Views: 202

qsrtech

Active Member
Licensed User
Longtime User
Sorry to hear that. It does work great for me. It's a bit of a hack but it is nice to have a switch when needed. Good luck!
 
Top