Android Question Search CustomType into a list

josejad

Expert
Licensed User
Longtime User
Hi:

I'm trying to find a element of a custom type in a list of them. As suggested in:
https://www.b4x.com/android/forum/threads/indexof-type.
B4X:
Type TipoSite (Site As String, Tecnologia As String, TipoInstalacion As String, FechaChkl As String)

I have a customlistview, and I've added a label with a trash to delete the item, a folder created and the element in the list to save in KVS

I've set the tag of the label to an element of my custom type:
B4X:
LbTrash.Tag = CreateSite(Site,Tecnologia,Tipo)

And this is what I do when I press the trash:

B4X:
Sub LbTrash_click
    Dim lbl As Label = Sender
    Dim indexLista = -1 As Int
    For index=0 To lista.Size-1
        If lista.Get(index) = lbl.Tag Then
            indexLista= index
        End If
    Next
    Log(indexLista)
End Sub

I always get -1

Thanks
 

stevel05

Expert
Licensed User
Longtime User
Without seeing more of your code it is not possible to give a definite answer, but it looks like you are creating a new object of type TipoSite to add to the tag. As this is a new object it will never match the object in the list. So you will need to either add the object from the list to the label tag, or compare the strings that are held in the two objects.:

B4X:
DIm Site1 As TipoSite = lista.Get(index)
Dim Site2 As TipoSite = lbl.Tag
If Site1.Site = Site2.Site AND Site1.Tecnologia = Site2.Tecnologia Then ' Add a comparison for whatever fields are needed to make the match
    IndexLista = index
    Exit
End If
Next
Log(indexLista)
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Thanks stevel05, you're rigth, they are not the same object. It was a doubt I had, if they should be the same object or just match all the fields.

Thanks for the answer, will try your way
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Hi again:

I'm trying to figure it out how to get the value I need. Maybe the way I'm doing is more complex that it should be.

Here I populate the CustomListView
The value I need to get in my trash label is lista.Get(i)

B4X:
Sub LoadSites
    Activity.LoadLayout("sites")
    Activity.Title = "Listado Sites"
    If Starter.kvs.ContainsKey("ListaSites") Then 'Check if we have some sites saved, to show it
        lista.Initialize2(Starter.kvs.Get("ListaSites"))
        For i = 0 To lista.Size - 1
            Dim site = lista.Get(i) As TipoSite
            CLVSites.Add(CreateListItem(site.Site & CRLF & site.TipoInstalacion, CLVSites.AsView.Width, 60dip, site.FechaChkl, site.Tecnologia), lista.Get(i))
        Next
    Else
        lista.Initialize
        ToastMessageShow("There's no sites, click + to add one", True)
    End If
End Sub

The CreateListItem code:
B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int, Fecha As String, Tecnologia As String) As Panel
    Dim p As Panel
    'ImageView1 type is B4XView
    Dim Bitmap1 As Bitmap
    Bitmap1.Initialize(File.DirAssets, Tecnologia & ".jpg")
    p.Initialize("")
    p.SetLayout(0, 0, Width, Height)
    p.LoadLayout("cellSites")
    LbSite.Text = Text
    LbFecha.Text = Fecha
    ImgTecnologia.SetBitmap(Bitmap1)
    LbPapelera.Tag = p.GetView(0) 'Here is where I need to get the value¡¡ p.GetView(0) is just one test, I've tried Me...
    Log(LbPapelera.Tag)
    Return p
End Sub

And here is where I create my CustomType:

B4X:
Private Sub CrearSite(Site As String, Tecnologia As String, TipoInstalacion As String) As TipoSite
    Dim chklst As TipoSite
    chklst.Initialize
    chklst.Site = Site
    chklst.Tecnologia = Tecnologia
    chklst.TipoInstalacion = TipoInstalacion
    chklst.FechaChkl = DateTime.Date(DateTime.Now)
    Return chklst
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You are passing an object, lista.Get(i) to the sub CreateListItem as argument no 5 Tecnologia which us defined as a string.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Hi again stevel05:

I think lista.Get(i) is not an argument to CreateListItem, is the value of CLVSites.Add. Probably I should pass lista.Get(i) as one more argument
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Looking carefully, I realize I pass as argument to CreateListItem the members of the object, so it's easier (and probably the rigth way) pass the whole object:

B4X:
Sub CreateListItem(newSite as TipoSite) As Panel

Thanks¡¡
 
Upvote 0
Top