Android Question Spinner and radio button changing variables

I have two RadioButtons and a Spinner, all generated in the same layout. I want to change variables according to the selected item from the list and to the checked RadioButton. If the user selects one item from the spinner, the value of a variable will change no matter which radiobutton is checked, if one radiobutton is checked, the value of another variable will be changed in one way, if the other radiobutton is checked, the value that variable will be changed in an other way, etc...

I have created a sub for each item in the spinner, in each of these sub I have been creating a if condition for the radiobuttons. The code is repeated multiple times, I've been trying to find another solution and I ended up wasting my time without finding a proper way to do it, so I continued this way hoping it would work.

My problem is : when i log the variables after the user checks the radio button and selects an item, the variables' values are incorrect, way out of their proportion and I don't know why.

Here is an example :

B4X:
Sub Globals
Dim a As Int
Dim b As Int
Dim c As Int
Dim d As Int
Dim spinner1 As Spinner
Dim list1 As List : list1.initialize
Dim radiobutton1 As RadioButton
Dim radiobutton2 As RadioButton
End Sub

Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("mylayout")

    list1.AddAll(Array As String("item1","item2", "item3", "item4", "item5"))
    spinner1.AddAll(list1)
End Sub

Sub radiobutton1_CheckedChange(Checked As Boolean)

End Sub

Sub radiobutton2_CheckedChange(Checked As Boolean)

End Sub

Sub spinner1_ItemClick(Position As Int, Value As Object)
item1_selected(0,"item1")
item2_selected(1,"item2")
item3_selected(2,"item3")
item4_selected(3,"item4")
item5_selected(4,"item5")
End Sub

Sub item1_selected(Position As Int, Value As Object)
a = a + 6 'This value stays the same no matter which radiobutton is checked
If radiobutton1.Checked = True Then 'These are the variables I want to change if radiobutton1 is checked
b = b - 17
c = c + 12
End If

If radiobutton2.Checked = True Then 'And these if radiobutton2 is checked
 b = b + 23
 d = d - 8
 End If
 End Sub 'The code structure is the same for the other items only the values and the variables change

I am sorry for the long thread and i thank you in advance for your reply and for the accorded patience
 

klaus

Expert
Licensed User
Longtime User
I suggest you to define one routine for the calculation of the variables:
And call this routine from the Spinner and the RadioButtons.
B4X:
Private Sub spinner1_ItemClick(Position As Int, Value As Object)
    SelectedItem = Value
    Calculation
End Sub

Sub radiobutton1_CheckedChange(Checked As Boolean)
    If Checked = True Then
        Calculation
    End If
End Sub

Sub radiobutton2_CheckedChange(Checked As Boolean)
    If Checked = True Then
        Calculation
    End If
End Sub

Private Sub Calculation
    a = a + 6 'This value stays the same no matter which radiobutton is checked
    Select SelectedItem
        Case "item1"
            If radiobutton1.Checked = True Then 'These are the variables I want to change if radiobutton1 is checked
                b = b - 17
                c = c + 12
            End If
            If radiobutton2.Checked = True Then 'And these if radiobutton2 is checked
                b = b + 23
                d = d - 8
            End If
        Case "item2" 
        Case "item3"
    End Select
End Sub

And
 
Upvote 0
Thank you Klaus for the quick reply. I tested your code and it seems it works for my case the only problem I have is that if I check, uncheck and check again or check the other radiobutton, the value changes for each check, the same is happening every time I select an item from the spinner, the 'a' variable keeps adding 6. Is there a way I can solve this ?
 
Upvote 0

udg

Expert
Licensed User
Longtime User
If I understand you correctly, you want the "a" incremented only when a new selection is done using the spinner.
In that case, just move the a= a +6 istruction from Calculation to spinner1_ItemClick
 
Upvote 0
Thank you for the reply @udg , it seems i didn't explain myself correctly, i did increment the 'a' variable from the calculation to the spinner.

The problem is : every time I check a radiobutton it changes the value of the variable for each time I check it (for exemple if the user checks the radiobutton1 and then changes his mind and choses the radiobutton2, the variable changes two times, once for each check : radiobutton1 and radiobutton2). The same is happening with the spinner when i select an item from the spinner and then select an other one from that same spinner, the values of the variable seem to change for each time i select an item in the spinner.

I want the variables to change only once, according to which item and checkbox is selected.

I tried finding a solution for the couple days, but I can't find how I can set the condition so that the values changes only once, according to which radiobutton is checked and which item is selected. Is it possible to do something like this ? Sorry for making this a pain for you all.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Your best solution could be the definition of a global boolean variable. Once set following the first increment, the test will keep it from further increments

Something like
B4X:
Sub Process_Globals
Private ShouldIncrement as boolean = True
..
end sub
...
<same code as above post s but change this line>
if ShoulIncrement then
    a = a + 6
    ShouldIncrement = False
end if
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
I have not yet understood what exactly you want to do, but the attached project might go in the right direction.

I don't yet know when exactly the variable 'a' should be incremented by 6.
It should probably be calculated in function of the selected item in the Spinner.

Attached, my test project.
 

Attachments

  • SpinnerRadioButtons.zip
    9.8 KB · Views: 268
Upvote 0
Thank you @udg and @klaus for taking time to give me help and support, I think I finally found the solution to my problem, i did not increment the 'a' variable to the selected item in the spinner, thanks to your code @klaus I was able to understand where I messed up, thank you all very much and sorry for taking way too much of your time
 
Upvote 0
Top