Android Tutorial [B4X][B4XPages] SQL + xCLV Example

A simple, cross platform example that reads data from a database and adds it to a xCLV.

1624428141587.png


Database source: https://www.sqlitetutorial.net/sqlite-sample-database/

The interesting code:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    B4XPages.SetTitle(Me, "SQL + xCLV Example")
    xui.SetDataFolder("sql example") 'required in B4J
    Dim dbname As String = "chinook_v1.db"
    If File.Exists(xui.DefaultFolder, dbname) = False Then File.Copy(File.DirAssets, "chinook.db", xui.DefaultFolder, dbname)
    #if B4J
    'B4J SQL object can access many types of databases (same as B4A JdbcSQL).
    sql.InitializeSQLite(xui.DefaultFolder, dbname, False)
    #else
    sql.Initialize(xui.DefaultFolder, dbname, False)
    #End If
    Dim rs As ResultSet = sql.ExecQuery2($"SELECT artists.name AS ArtistName, albums.title AS AlbumTitle FROM albums, artists 
        WHERE albums.artistid = artists.artistid"$, Null)
    Do While rs.NextRow
        Dim rd As RecordData = CreateRecordData(rs.GetString("ArtistName"), rs.GetString("AlbumTitle"))
        CustomListView1.Add(CreateItem(rd), rd)
    Loop
    rs.Close
End Sub

Private Sub CreateItem(rd As RecordData) As B4XView
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, Root.Width, 60dip)
    p.LoadLayout("Item")
    'labels will point to the last loaded views with the set name.
    lblAlbum.Text = rd.AlbumName
    lblArtist.Text = rd.ArtistName
    Return p
End Sub
 

Attachments

  • SQL + xCLV Example.zip
    347 KB · Views: 1,093

epiCode

Active Member
Licensed User
Thank you so much Erel - This is very helpful ???
 

TILogistic

Expert
Licensed User
Longtime User
As "few" of you know, I'm mostly a nuisance ? , so...

A good example would also be the "porting" of SQLiteViewer for B4XPages, with B4XTable.

It would only be necessary to convert to B4XPages ????


another option for @LucaMs wishes, it would be JRDC2 to B4Xtable and B4XCustomListview

 
Last edited:
Top