Android Question ListView and ToggleButton from database

lamsri799

Member
Licensed User
Longtime User
i am select data from database
if status = 1 togglebutton = on
else status =2 togglebutton = off
but load avtivity
all togglebutton is similar
how to load avtivity so all togglebuttons is on or off as status

Sorry for language i am beginner

My code
B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
   
    Dim p As Panel
    p.Initialize("")
    p.Color = Colors.Black
    Dim b As Button
    b.Initialize("button") 'all buttons click events will be handled with Sub Button_Click
    Dim chk As ToggleButton
    chk.Initialize("chk")
If dbsql.IsInitialized = False Then dbsql.Initialize(File.DirInternal,"money.db",True)
    mccursor = dbsql.ExecQuery("Select status FROM control  WHERE user_id = '"& id_login &"'order by control_id ")
        If mccursor.RowCount > 0 Then
            For i = 0 To mccursor.RowCount -1
            mccursor.Position =i
            Dim status As Int
            status = mccursor.GetInt ("status")
        If status = 1 Then
            chk.Checked = True
            chk.TextColor = Colors.Green
            chk.TextOn = "ON"
        Else If status = 2 Then
            chk.Checked = False
            chk.TextColor = Colors.Red
            chk.TextOff = "OFF"
        End If
        Next
        End If

    Dim lbl As Label
    lbl.Initialize("")
    lbl.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.LEFT)
    lbl.Text = Text
    lbl.TextSize = 16
    lbl.TextColor = Colors.White
    b.Text = "Delete"
    p.AddView(chk, 1%x, 2dip, 20%x, Height - 1dip) 'view #0
    p.AddView(lbl, 24%x, 2dip, 60%x, Height - 1dip) 'view #1
    p.AddView(b, 84%x, 2dip, 16%x, Height - 1dip) 'view #2
    Return p

End Sub
 

lamsri799

Member
Licensed User
Longtime User
What is the problem in the above code?
B4X:
If dbsql.IsInitialized = False Then dbsql.Initialize(File.DirInternal,"money.db",True)
    mccursor = dbsql.ExecQuery("Select status FROM control  WHERE user_id = '"& id_login &"'order by control_id ")
        If mccursor.RowCount > 0 Then
            For i = 0 To mccursor.RowCount -1
            mccursor.Position =i
            Dim status As Int
            status = mccursor.GetInt ("status")
        If status = 1 Then
            chk.Checked = True
            chk.TextColor = Colors.Green
            chk.TextOn = "ON"
        Else If status = 2 Then
            chk.Checked = False
            chk.TextColor = Colors.Red
            chk.TextOff = "OFF"
        End If
        Next
        End If
all data select

example
data 1 status = 1
data 2 status = 2
data 3 status = 1

result is

all data togglebutton is ON
i want to result data 1 togglebutton is ON
data 2 togglebutton is OFF
data 3 togglebutton is ON
 
Upvote 0
Top