Android Question Selectedindex_changed only fires after the second change when using B4XComboBox1.SetItems(lstItems)

mscientist33

Active Member
Licensed User
I am using this code provided by @Mahares which works as expected Except the B4XComboBox1_SelectedIndexChanged only fires AFTER the second change.

B4X:
Dim lstItems As List

    lstItems.Initialize  

    Dim rs As ResultSet

    Do While rs.NextRow

        If rs.GetString("n").Contains("1") Then

             lstItems.Add(cs.Initialize.Color(Colors.Green).Append(rs.GetString("n")).PopAll)

        Else

            lstItems.Add(cs.Initialize.Color(Colors.Red).Append(rs.GetString("n")).PopAll)          

        End If

    Loop

    rs.close

    B4XComboBox1.SetItems(lstItems)


I have found the same issue from this thread but it was never answered. Same issue
 
Last edited:

chaurcey

Member
I am using this code provided by @Mahares which works as expected Except the B4XComboBox1_SelectedIndexChanged only fires AFTER the second change.

B4X:
Dim lstItems As List

    lstItems.Initialize

    Dim rs As ResultSet

    Do While rs.NextRow

        If rs.GetString("n").Contains("1") Then

             lstItems.Add(cs.Initialize.Color(Colors.Green).Append(rs.GetString("n")).PopAll)

        Else

            lstItems.Add(cs.Initialize.Color(Colors.Red).Append(rs.GetString("n")).PopAll)        

        End If

    Loop

    rs.close

    B4XComboBox1.SetItems(lstItems)


I have found the same issue from this thread but it was never answered. Same issue
Greetings,

Yeah, as I understand it, the selected index change only fires when there is a change of selection. Since the first item is set as the selected item with an index of 0 pressing it generates no change so the change event is not generated.

I have a list of URLs so what I do is have the 0 index item with the following placeholder text:

Items.Add("Select a URL from list to view")

That works for me but may or not fit your list functionality.

An alternative which may be more in line with your functionality is to set the selected index to -1 after you have added the list of items.

For me that would be cbURLs.SelectedIndex = -1

Then when the 0 index item is selected the event will fire as the index will change to 0. That seems to work as I just did a quick test.

Hope this helps!
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
B4XComboBox1_SelectedIndexChanged only fires AFTER the second change.
Works as expected, no issues:
B4X:
B4XComboBox1.SetItems(lstItems)
B4XComboBox1_SelectedIndexChanged(0)  'call it directly

Sub B4XComboBox1_SelectedIndexChanged (Index As Int)
    Log(B4XComboBox1.GetItem(Index))  'it will display here the item with index 0. 
End Sub
 
Upvote 0
Top