Android Question Add a variable in a customListView

Lego Jérôme

Member
Licensed User
For my project, I use an SQL database, each line contains all the data of a panel to add to my customListView.


On my panel of customListView I have to enter the date with this form: "21 Nov 2018"
I managed to add my SQL lines to my customListView with this sub:
B4X:
Private Sub OpenActiveFile As Boolean
    If File.Exists(PathPhone,ActiveFile) = False Then
        Serveur.Download(ActiveFile)
        Return False
    End If
    
    If SQLactif.IsInitialized = False Then
        SQLactif.Initialize(PathPhone, ActiveFile, False)
    End If
    
    For i=0 To CommandeSQL.NbLigne(SQLactif)-1
        Dim l As LigneSQL = CommandeSQL.ExtractLigne(SQLactif,i+1)
        Dim iv As itemValue
        iv.Initialize
        CLV.Add(CreateItemLigneCompte(Fonction.DateNumToStr(l.date),NomUtilisateur,l.nature,l.prix,l.sended,iv),60dip,iv)
        DateOnIndex.Put(i,l.date)
        If l.sended = 0 Then
            SQLneedUpload = True
        End If
    Next
    Log($"
La liste : ${ActiveFile} à bien été ajoutée
"$)
    Return True
End Sub

Public Sub DateNumToStr(date As Date) As String
    Dim moisStr As String
    Select date.mois
        Case 1
            moisStr="Janv."
        Case 2
            moisStr="Fev."
        Case 3
            moisStr="Mars"
        Case 4
            moisStr="Avril"
        Case 5
            moisStr="Mai"
        Case 6
            moisStr="Juin"
        Case 7
            moisStr="Juil."
        Case 8
            moisStr="Août"
        Case 9
            moisStr="Sept."
        Case 10
            moisStr="Oct."
        Case 11
            moisStr="Nov."
        Case 12
            moisStr="Dec."
    End Select
    Dim r As String = date.jour & " " & moisStr & " " & date.annee
    Return r
End Sub

And for insert a new panel in my list view I would like to insert it in chronological order.

At first I have the date in three variables:
  • days
  • month
  • year

I typed a new variable named Date :
B4X:
Type Date (jour As Int, mois As Int,annee As Int)

I intend to create a sub that compares the dates and returns the most recent.

My problem is that once the panel is added to my customListView, I can not get my Date variable.
I could only get the date with an iv.lbl.text in the form:"21 Nov 2018"

I would like to find a way to "attach" my Date variable to my panel.
Do you have a track to give me?

I would prefer to avoid having to create three invisible labels to store these variables.
Because I think it's going to take a long time to deal with.
 

LucaMs

Expert
Licensed User
Longtime User
Neglecting the fact that there are better ways to work with dates (and also to get the names of the months), no one forces you to fill the CLV while reading data from the database (and in fact it is also a wrong thing to do, in my opinion). You can load the data in a "List of custom types", sort it as you want and then fill the CLV with this data, already sorted.
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Please, allow me a note. Seens that you arer using a incorrect way to deal com dates.
See a example how get month name.

B4X:
Dim dt As String="2018-11-20"
DateTime.DateFormat="yyyy-MM-dd"
Dim dl As Long=DateTime.DateParse(dt)
DateTime.DateFormat="MMMM" ' Or MMM for short name.
  
Log(DateTime.Date(dl))
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Please, allow me a note. Seens that you arer using a incorrect way to deal com dates.
See a example how get month name.

B4X:
Dim dt As String="2018-11-20"
DateTime.DateFormat="yyyy-MM-dd"
Dim dl As Long=DateTime.DateParse(dt)
DateTime.DateFormat="MMMM" ' Or MMM for short name.
 
Log(DateTime.Date(dl))

Log(DateUtils.GetMonthName(dl))

DateUtils is a library (no need to search for it, it is included in the b4a installation).
 
Upvote 0

Lego Jérôme

Member
Licensed User
Great thank you both, I did not know that we could treat the date with "DateTime."
LucasMs, I would like to follow your method but I do not see how to sort a custom list by date.

could you show me an example?
In the meantime I will try to find this method.
 
Upvote 0
Top