read From listview not returning the correct result

farisnt

Member
Licensed User
Longtime User
Hi ,
I am very new so it may be silly, but here is the q
I have:
1- Listview (contain 2 lines "1"and "2") only
2- 2x Togglebutton
3- button

The button should read the list and incase the first line value is 1 the toggle should be check=True , Else Toggle.check= False

when I press the button, the returned value are incorrect.
whats up
:sign0163::sign0163:
 

Attachments

  • Listview.zip
    7.1 KB · Views: 256

Mahares

Expert
Licensed User
Longtime User
Based on your question, the code checks the 1st line in listview. if it holds 1 then toggle true, else toggle false.
Copy and paste all my code shown below over top all your code and check it out. See if that is what you are after:
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   
   Dim add1 As Button
   Dim list1 As ListView
   Dim restore As Button
   Dim et As EditText
   Dim Tog1 As ToggleButton
   Dim Tog2 As ToggleButton

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   Activity.LoadLayout ("main")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub restore_Click
    'Checks the 1st line in listview. if it holds 1 then toggle true, else toggle false.
      list1.Clear
      Dim i As Int
      For i=0 To 1
         list1.AddSingleLine (Rnd(0,2))
      Next
'      Msgbox("Press ok to continue.","")
      Dim MyValue As Int
      MyValue=list1.GetItem(0)
      If MyValue=1 Then
         Tog1.Checked=True :Tog2.Checked=True 
      Else
         Tog1.Checked=False  :Tog2.Checked=False
      End If
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can do it in a much simpler way:
B4X:
Sub restore_Click
    Dim aa As Int
    
    For aa = 0 To list1.Size - 1
        If list1.GetItem(aa) = "1" Then
            Tog1.Checked=True 
        Else 
            Tog1.Checked=False
        End If
    Next
End Sub
The problem in your code is not the ListView returning the wrong value it's that you check a wrong value!
The content of the ListView are strings but you compare it to a number.
If list1.GetItem(aa) = 1 Then
must be
If list1.GetItem(aa) = "1" Then


But I don't understand the purpose of this routine because you will see only the result of the last line and the execution so fast that you won't see the intermediate states.

But if you want to have the state of a line when select it you should use a code like this using the ListView_ItemClick event:
B4X:
Sub list1_ItemClick (Position As Int, Value As Object)
    If Value = "1" Then
        Tog1.Checked=True 
    Else 
        Tog1.Checked=False
    End If
End Sub
Best regards.
 

Attachments

  • Listview1.zip
    7.2 KB · Views: 259
Upvote 0

farisnt

Member
Licensed User
Longtime User
Thanks ,
There will be another 30 Toggle box and the value they will have will be stored on the listview,
so every togglebox will have a single value
Tog1 will have the List1.Getitem(0)
Tog1 will have the List1.Getitem(1)
Tog1 will have the List1.Getitem(2)

and so.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
When you ask a question with an example you should expose the whole problem.
In your example code you have only two entries in the ListView and two ToggleButtons but you change only ToggleButton1 !?

Tog1 will have the List1.Getitem(0)
Tog1 will have the List1.Getitem(1)
Tog1 will have the List1.Getitem(2)
Sorry I don't understand. Tog1 is supposed to have what value ?
Could you explain exactly what you want to do.

If it should be like this:
B4X:
If List1.Getitem(0) = "0" Then
    Tog1.Checked = False
Else
    Tog1.Checked = True
End If

If List1.Getitem(1) = "0" Then
    Tog2.Checked = False
Else
    Tog2.Checked = True
End If
Then you should use an array for the ToggleButtons and use a loop. Something like this:
B4X:
For i = 0 To List1.Size - 1
  If List1.Getitem(i) = "0" Then
    Tog(i).Checked = False
   Else
     Tog(i).Checked = True
  End If
Next
Best regards.
 
Last edited:
Upvote 0

farisnt

Member
Licensed User
Longtime User
I am very sorry for the mistype
Tog1 is a toggle button
and its not
Tog1 will have the List1.Getitem(0)
Tog1 will have the List1.Getitem(1)
Tog1 will have the List1.Getitem(2)
its
Tog1 will have the List1.Getitem(0)
Tog2 will have the List1.Getitem(1)
Tog3 will have the List1.Getitem(2)

Each toggle button on the activity will read a spicifec value in the listview
Sorry again
 
Upvote 0

farisnt

Member
Licensed User
Longtime User
Thanks alot, this is a great Example
I guess I will have several other Qs these days,
I really like Basic4Android
 
Upvote 0
Top