Italian Convertire una CustomListView ad una CustomListView con lazy loading caricando i dati risultato di httpjob

Schakalaka

Active Member
Licensed User
Longtime User
buongiorno a tutti
ho da un po questo problema, che anche utilizzando chat gpt non riesco a risolvere.
Vorrei aggiungere una custom list view il lazy loading.
La customlistview, carica i dati ottenuti da un httpjob in json.


B4X:
Sub downloadEmailsLista
    
    UsersCV.Clear
    Dim bytemail() As Byte
    Dim byteSalsy() As Byte

    Dim index As Int = 1

    e.Initialize
    Dim j As HttpJob
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.Download(ServerURl & "/xxxxxxxxxxxxxx/restapi.php?api=tbl-users&orderby=my_iduser&sort=asc") '&orderby=my_iduser&sort=asc") '2")
    ProgressDialogShow2("Loading User list...",False)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)

        Dim parser As JSONParser
        parser.Initialize(j.getstring)
        Dim jRoot As List = parser.NextArray
        For Each coljRoot As Map In jRoot
            my_enabled = coljRoot.Get("my_enabled")
            my_appversion = coljRoot.Get("my_appversion")
            my_email = coljRoot.Get("my_email")
            my_last_see = coljRoot.Get("my_last_see")
            my_btcaddress = coljRoot.Get("my_btcaddress")
            my_ispremium = coljRoot.Get("my_ispremium")
            Dim my_salt As String = coljRoot.Get("my_salt")
            Dim my_registration_date As String = coljRoot.Get("my_registration_date")
            Dim my_iduser As Int = coljRoot.Get("my_iduser")
            
            byteSalsy = encode.DecodeBase64(my_salt)

            Dim stringSalt As String = DecryptText(byteSalsy,Main.app_id)

            my_salt = stringSalt

            bytemail = encode.DecodeBase64(my_email)
    
            stringmail = DecryptText(bytemail,my_salt)
            e.Add(stringmail)
    
            DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss"
            Dim dt As Long = DateTime.DateParse(my_last_see)
            DateTime.DateFormat = "dd/MM/yyyy"
'            Log(DateUtils.PeriodBetweenInDays(DateTime.DateParse($"$Date{dt}"$),DateTime.Now).Days)
        
            Dim daydif As Int = DateUtils.PeriodBetweenInDays(DateTime.DateParse($"$Date{dt}"$),DateTime.Now).Days
       
            UsersCV.Add(CreateItem(UsersCV.AsView.Width, my_iduser, stringmail, daydif,my_appversion), "")

        
        Next

    

    Else
        ToastMessageShow("Error: " & j.ErrorMessage, True)
    End If
    j.Release

    ProgressDialogHide
End Sub

B4X:
Private Sub CreateItem(Width As Int, userid As String, email As String, last As String, version As String) As Panel
Dim p As B4XView = xui.CreatePanel("")
Dim height As Int = 160dip 'UsersCV.AsView.Height '350dip  '50dip
p.SetLayoutAnimated(0, 0, 0, Width, height)
p.LoadLayout("CLV_UTENTI")
    p.Color = Colors.Cyan
   
If my_enabled = 0 Then
Panel1.Color = 0xAEFF0000
End If
lbluserID.Text = userid 'user id da searchbox
lblemail.Text = email 'email decriptata
       
    lbllastactivity.Text = "Ultima attività: "
txtastactivity.Text = last 'differenza in giorni della ultima attività
   
lblversion.Text = "Versione: "
    txtversion.Text = version
    index = userid
Return p

End Sub



B4X:


Questo è il codice fornito da chat gpt, dove imposta jRoot come variabile globale per riutilizzarla

B4X:
Sub downloadEmailsLista
    ' Initialize the variables.
    Dim UsersCV As CustomListView
    Dim bytemail() As Byte
    Dim byteSalsy() As Byte
    Dim index As Int = 1
    Dim e As List
    e.Initialize
    ' Create the custom list view.


    ' Load the data from the API.
    Dim j As HttpJob
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.Download(ServerURl & "/clickandclaim-host/restapi.php?api=tbl-users&orderby=my_iduser&sort=asc")
    ProgressDialogShow2("Loading User list...",False)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)

        Dim parser As JSONParser
        parser.Initialize(j.getstring)
        jRoot  = parser.NextArray
    Else
        ToastMessageShow("Error: " & j.ErrorMessage, True)
    End If
    j.Release

    ProgressDialogHide


End Sub


B4X:
Sub JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)

        Dim parser As JSONParser
        parser.Initialize(j.getstring)
        jRoot  = parser.NextArray
    Else
        ToastMessageShow("Error: " & j.ErrorMessage, True)
    End If
    j.Release

    ProgressDialogHide
End Sub

B4X:
Private Sub UsersCV_LazyLoad (FirstIndex As Int, LastIndex As Int)
    Dim bytemail() As Byte
    Dim byteSalsy() As Byte
    Dim daydif As Int = 0

    Dim ExtraSize As Int = 20

    For i As Int = Max(0, FirstIndex - ExtraSize) To Min(LastIndex + ExtraSize, UsersCV.Size - 1):
        If i >= jRoot.Size Then Exit

        Dim coljRoot As Map = jRoot.Get(i)
        my_enabled = coljRoot.Get("my_enabled")
        my_appversion = coljRoot.Get("my_appversion")
        my_email = coljRoot.Get("my_email")
        my_last_see = coljRoot.Get("my_last_see")
        my_iduser = coljRoot.Get("my_iduser")

        UsersCV.Add(CreateItem(UsersCV.AsView.Width, my_iduser, my_email, daydif, my_appversion), "")
    Next
 
End Sub

Restituisce l' errore "waiting for regular variable followed with "="


B4X:
Private Sub CreateItem(Width As Int, userid As String, email As String, last As String, version As String) As Panel
    Dim p As B4XView = xui.CreatePanel("")
    Dim height As Int = 160dip 'UsersCV.AsView.Height '350dip  '50dip
    p.SetLayoutAnimated(0, 0, 0, Width, height)
    p.LoadLayout("CLV_UTENTI")
    p.Color = Colors.Cyan

    lbluserID.Text = userid
    lblemail.Text = email

    lbllastactivity.Text = "Ultima attività: "
    txtastactivity.Text = last

    lblversion.Text = "Versione: "
    txtversion.Text = version
    index = userid
    Return p
End Sub

B4X:
Sub UsersCV_VisibleRangeChanged(FirstVisible As Int, LastVisible As Int)
    ' Lazy load the data for the visible range
    Dim Items() As Object
    Items = LoadData(FirstVisible, LastVisible)
    ' Add the items to the custom list view.
    For i = FirstVisible To LastVisible
        UsersCV.Add(Items(i), "")
    Next
End Sub

B4X:
Sub LoadData(FirstVisible As Int, LastVisible As Int) As Object()
    ' Create an array to store the data
    Dim Items() As Object
    ' Iterate over the visible range
    For i = FirstVisible To LastVisible
        ' Add the data to the array
        Items = Array(i)
    Next
    ' Return the array
    Return Items
End Sub

codice alternativo
B4X:
Sub UsersCV_LazyLoad(FirstIndex As Int, LastIndex As Int)

Dim ExtraSize As Int = 20
Dim daydif As Int
Dim items() As Object

If jRoot.IsInitialized Then
    items = jRoot.GetRange(FirstIndex, LastIndex + 1)
    For i As Int = 0 To items.Size - 1
        daydif = DateUtils.GetDaysBetween(DateTime.Now, items(i).Get("my_last_see"))
        UsersCV.Add(CreateItem(UsersCV.AsView.Width, items(i).Get("my_iduser"), items(i).Get("my_email"), daydif, items(i).Get("my_appversion")), "")
    Next
End If

End Sub
{/code]

ma da continuamente errore

Magari qualcuno ha già avuto questa necessità.
Grazie
 

LucaMs

Expert
Licensed User
Longtime User
Ho provato anch'io a sfruttare la cosiddetta "Intelligenza artificiale" tanto osannata (e temuta) in questo periodo. Ho chiesto codice sorgente a Perplexity: sempre gran parte sbagliato! Ergo, direi che, almeno per il momento, meglio non perdere tempo con ChatGPT.

Il lazy loading non è certo semplicissimo; partirei da questo esempio di Erel e lo adatterei alle mie esigenze.
 

Schakalaka

Active Member
Licensed User
Longtime User
sono riuscito a creare questo progettino
adesso vorrei che al click, apra un' altra view con i dettagli.

Sarebbe piu pratico, salvare tutta la lista degli utenti con tutti i dati necessari su un file txt e caricare le clv utilizzando quello?
 

Attachments

  • Customlist lazy http.zip
    15 KB · Views: 134
Top