Android Question [solved] Strange behaviour spinner

angel_

Well-Known Member
Licensed User
Longtime User
I want that when pressing a RadioButton does not change the Spinner if that item is included in the list, in case it is not included that I change it.

Examples:

- If I select in the Spinner1 "2.5" (SelectedIndex = 4) and press the RadioButton2 as it is included in the ListItems2 positions in the Spinner1 the SelectedIndex = 1, and if I press RadioButton1 again the Spinner1 keeps its value but as the elements ListItems1, this works correctly.

- If I select in the Spinner1 "2" (SelectedIndex = 3) and press the RadioButton2 Spinner1 does not change (it is correct) and includes the ListItems2 list (correct), but if I press RadioButton1 it takes me to SelectedIndex = 0, when I should keep it in the SelectedItem value = "2", this is not correct.

I'm sorry about my English.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Panel1 As Panel
    Private RadioButton1 As RadioButton
    Private RadioButton2 As RadioButton
    Private Spinner1 As Spinner
    
    Private ListItems1() As String = Array As String("0.5", "1", "1.5", "2", "2.5", "3")
    Private ListItems2() As String = Array As String("2", "2.5", "3")
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("1")

    RadioButton1.Checked = True
    
    Spinner1.Clear
    Spinner1.AddAll(ListItems1)
    Spinner1.SelectedIndex = 0
End Sub

Sub RadioButton1_2_CheckedChange(Checked As Boolean)
    Items(RadioButton1, Spinner1)
End Sub

Sub Items(rbt As RadioButton, spn As Spinner)
    Dim ItemCurrent As String
    Dim List1 As List
    
    If spn.SelectedIndex > 0 Then
        ItemCurrent = spn.SelectedItem
    Else
        ItemCurrent = ""
    End If
    
    List1.Initialize
    If rbt.Checked Then
        List1 = ListItems1
    Else
        List1 = ListItems2
    End If

    spn.Clear
    spn.AddAll(List1)
    spn.SelectedIndex = Max(spn.IndexOf(ItemCurrent), 0)
End Sub
 

Attachments

  • Test1.zip
    9.4 KB · Views: 286

DonManfred

Expert
Licensed User
Longtime User
Try

B4X:
spn.Clear
    If rbt.Checked Then
        spn.AddAll(ListItems1)
    Else
        spn.AddAll(ListItems2)
    End If
 
Upvote 0

angel_

Well-Known Member
Licensed User
Longtime User
I tried but it does not work

B4X:
Sub Items(rbt As RadioButton, spn As Spinner)
    Dim ItemCurrent As String
    
    If spn.SelectedIndex > 0 Then
        ItemCurrent = spn.SelectedItem
    Else
        ItemCurrent = ""
    End If
    
    spn.Clear
    If rbt.Checked Then
        spn.AddAll(ListItems1)
    Else
        spn.AddAll(ListItems2)
    End If
    
    spn.SelectedIndex = Max(spn.IndexOf(ItemCurrent), 0)
End Sub

I happen the same, with "2.5" or "3" it works but with "2" (Index = 0) does not work.
 
Upvote 0
Top