Android Question Only add item to listview if it's not already on the list

ElliotHC

Active Member
Licensed User
I'm trying to create a list where I need to add entries, but I need to check the list first and only add the item if it's not already on the list.

Please could someone post an example of this as B4A has a slightly different syntax to VB. I would normally do something like this:

For Each item As ListItem In MyListBox.Items

If item.Selected Then
'Compare to new item
' Set a flag if it apears
End If

Next

If the Flag isn't set then
Add the item
 

ElliotHC

Active Member
Licensed User
B4X:
Dim Count As Int
Dim Flag As Boolean = 0

        For Count = 0 To ListView_Connections.Size
            If Starter.ConnectedName = ListView_Connections.GetItem(Count) Then
                Flag = 1
            End If
        Next

        If Flag = 1 Then
            ListView_Connections.AddSingleLine(Starter.ConnectedName)
        End If

Is that right? As in, is that the most efficient way to do it?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Supposing you used a list to fill the listVIew, name it lstItems for example, you could do this:
B4X:
if lstItems.indexof(Starter.ConnectedName)=-1 then 
   lstItems.add(Starter.ConnectedName)
   ListView_Connections.AddSingleLine(Starter.ConnectedName)
end if
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…