Android Question Maps / Lists only saving last record.

JonnyCav

Member
I've been at this for 8 hours! Instead of each checkbox being submitted individually to my sqlite database I'm trying to put them into a map (for each checked) and then (hopefully) insert them under one button click.

The 'map' seems to be working correctly (key gets updated and the array is correct. But when I try to go through them as per Erel's 'For Each...' it only returns the last checked entry (albeit in the correct format/key number)

(When the checkbox is checked LOG) returns
(MyMap) {4=[87, ME21, Алферова Ольга, 03-01-24, 4, P]}
(MyMap) {5=[88, ME21, Ашыров Шаназар, 03-01-24, 4, P]}
(MyMap) {6=[89, ME21, Горяинова Яна, 03-01-24, 4, P]}

(For Each.....) returns
[89, ME21, Горяинова Яна, 03-01-24, 4, P]

Can anyone spot my (probably blatant) mistake?

B4X:
rivate Sub cbStudents_CheckedChange(Checked As Boolean)

'WORKING CODE BELOW

    Dim index As Int = clvAttendance.GetItemFromView(Sender)
    Dim p As B4XView =clvAttendance.GetPanel(index)
    Dim CB As CheckBox = Sender
    
    'Dim records As Map
    records.Initialize
    counter=counter+1
    
    
    Dim studentsToAdd As List
    studentsToAdd.Initialize
    
    If CB.Checked  Then
        lblstudentID=(p.GetView(0).Text)
        
        studentsToAdd.AddAll(Array(lblstudentID,selectedGroup,CB.Text, txtClassDate.Text, txtClassPeriod.Text, "P"))

        records.Put(counter,studentsToAdd)
        
    End If   

'Log(studentsToAdd)
Log(records)


'***** WORKING for individual CHECKS on CHECKBOXES ************

    'Starter.sql.ExecNonQuery2("INSERT INTO attendance VALUES (?, ?, ? ,?, ?, ?,null)", Array As Object(lblstudentID, selectedGroup, CB.Text, txtClassDate.Text, txtClassPeriod.Text, "P"))
        
    ToastMessageShow("Entry added", False)
'**************************************************************

End Sub


'MAIN SUB PAGE  - ATTENDANCE STEP 5 
'SUbmit all data to ATTENDANCE and UPDATE the 'classCount' table

Private Sub btnUpdateAttendance_Click
    
    'TEST OF MAP (records) **********
    
    For Each k As Int In records.Keys
        Dim v As String = records.Get(k)
        Log(v)
    Next
    ......
 
Solution
project that will automatically run and produce a debug log of what happens

Replace your Sub Activity_Create( with the below one, your will be good to go. It will show you 3 separate records:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    rs=Starter.sql.ExecQuery("SELECT studentID, studentName FROM students WHERE groupName='MO02'")
    ListOfLists.Initialize
    Dim i As Int
    Do While rs.NextRow        
        get_month_period
        Dim l1 As List
        l1.Initialize        
        l1.AddAll(Array(rs.GetString("studentID"),"MO02",rs.GetString("studentName"), cdate, year, "P", i))
        ListOfLists.Add(l1)
        i=i+1
    Loop
    rs.Close
    
    For Each list As List In...

JonnyCav

Member
Mahares You put the 'i' in the 'addall' and this was it. Amazing.
Thank you so much.
I'll try to parse how an 'add' after and 'addall' didn't work and I will.

I believe this is solved (and I'm sure will benefit future converts!)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I'll try to parse how an 'add' after and 'addall' didn't work and I will.
The add after an addall works. See below another way to get your results:
B4X:
ListOfLists.Initialize
    Dim i As Int
    Do While rs.NextRow        
        get_month_period
        Dim l1 As List  'you have to dim the l1 inside the loop so you do not keep overwriting records.
        l1.Initialize        
        l1.AddAll(Array(rs.GetString("studentID"),"MO02",rs.GetString("studentName"), cdate, year, "P"))
        l1.Add(i)
        ListOfLists.Add(l1)
        i=i+1
    Loop
    rs.Close

By the way on another aspect of your project and not to overwhelm you, it is better and safer to use a parameterized query. For example:
B4X:
rs=Starter.sql.ExecQuery("SELECT studentID, studentName FROM students WHERE groupName='MO02'")
Should be written as a parameterized query:
B4X:
rs=Starter.sql.ExecQuery2("SELECT studentID, studentName FROM students WHERE groupName= ?", Array As String("MO02"))
 
Upvote 0

JonnyCav

Member
The add after an addall works. See below another way to get your results:
B4X:
ListOfLists.Initialize
    Dim i As Int
    Do While rs.NextRow       
        get_month_period
        Dim l1 As List  'you have to dim the l1 inside the loop so you do not keep overwriting records.
        l1.Initialize       
        l1.AddAll(Array(rs.GetString("studentID"),"MO02",rs.GetString("studentName"), cdate, year, "P"))
        l1.Add(i)
        ListOfLists.Add(l1)
        i=i+1
    Loop
    rs.Close

By the way on another aspect of your project and not to overwhelm you, it is better and safer to use a parameterized query. For example:
B4X:
rs=Starter.sql.ExecQuery("SELECT studentID, studentName FROM students WHERE groupName='MO02'")
Should be written as a parameterized query:
B4X:
rs=Starter.sql.ExecQuery2("SELECT studentID, studentName FROM students WHERE groupName= ?", Array As String("MO02"))
I have done in the main project. In this example I just gave you a fixed groupname for simplicity. Thanks.
 
Upvote 0
Top