B4J Question Clearing and Renewing the Data of a B4XTable

B4XDev

Member
Licensed User
I have a B4XTable populated from an SQLite database with 19,056 records/rows.

When the app starts, it displays everything just fine... all records from 2005 to 2022.

At times, I need to be able to refresh the table with ONLY new 2022 data. Here's my attempt:

B4XTable Refresh Sub:
Private Sub btn_Refresh_Click
    Log("Emptying B4XTable")
    tbl_Codes.SetData(Array())
    tbl_Codes.UpdateTableCounters
'    tbl_Codes.ClearDataView
'    TableSelections.Clear
    Log("Building SQLite Database from legacy files")
    Log("Cleaning 2022 data")
    Clean2022FromDatabase
    Log("Converting legacy text files")
    ConvertLegacyCodesToSQliteDB
    Log("Loading new SQLite database")
    LoadCacheFromSqlite
    Log("Populating B4XTable")
    wait for (tbl_Codes.SetData(keysList)) complete (res As Object)
    Log("Refreshing B4XTable")
    tbl_Codes.RefreshNow
    Log("Done!")
End Sub

First I clear the data of the B4XTable with SetData(Array()).

Then I remove the current 2022-dated records from the SQLite database.

Then I add back to the SQlite database the updated 2022 records.

Then I reload all the data from the SQLite database into an array.

Then I re-populate the B4XTable with the new SQlite data.

What happens on the display is I end up seeing TWO copies of each 2022 record.

If I restart the app, those duplicates are gone. Everything looks normal again.

What am I doing wrong when re-populating the B4XTable and getting the duplicate 2022 records?
 

B4XDev

Member
Licensed User
Maybe this function reload old data?

Your question lead me to the problem!

The original function did not have the highlighted line below, which is the list created upon app startup with the current SQLite database.

LoadCacheFromSQLite w/Bug:
Private Sub LoadCacheFromSqlite
    Private recs As ResultSet
    keysList.Clear        ' THIS LINE WAS MISSING FROM THE ORIGINAL SUB
    recs = sql.ExecQuery("SELECT code,datetime,company,username,email,used,forms_list FROM codes ORDER BY datetime DESC;")
    Do While recs.NextRow
        Private newRec As List
        newRec.Initialize
        newRec.Add(recs.GetString("datetime"))
        newRec.Add(recs.GetString("code"))
        newRec.Add(recs.GetString("company"))
        newRec.Add(recs.GetString("username"))
        newRec.Add(recs.GetString("email"))
        newRec.Add(recs.GetString("used"))
        newRec.Add(recs.GetString("forms_list"))
        keysList.Add(newRec)
    Loop
End Sub

What's confusing, though, is why didn't every record get duplicated upon refresh?

Oh, well. It seems to be working fine now.

Thank you for the help!
 
Upvote 0
Top