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
In fact, I have have been logging the .SIZE of the map and despite all entries appearing to be unique, the .Size remains at 1 so maybe it's the 'put' that's not working?
 
Upvote 0

JonnyCav

Member
Maps and Lists are confusing me! My code above has a list which, I believe should be one whole record? I'm trying to create a map so that I can have each 'list' as a separate record.
My goal is to collect each 'list' for every checkbox ticked and then be able to insert them into a database by clicking once on the 'update' button. (instead of now it runs the sql insert each time a box is ticked. It's untidy!
 
Upvote 0

JonnyCav

Member
Erel. Thanks for your time but I'm obviously still missing something.
This (using 'to 100') just spits out the last checked box array 100 times and addends 1...100 as an extra column!
I can see what the intention is and I've been playing around inside and outside my 'If CB.Checked...' but it's not doing anything but repeating the last entry.
 
Upvote 0

JonnyCav

Member
Sorry Erel. I deleted all the code that was causing my hair to fall out. I will try to get round to adding it all again and send you the project. again. The project loaded above will show you.
Just click on 'Attendance' on the main screen and then select a Group and then checkbox any name. The error remains the same.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Just click on 'Attendance' on the main screen and then select a Group and then checkbox any name. The error remains the same.
Your project is a monster. You are trying to tackle too many things at the same time. You are biting off a lot more than you can chew, especially for some one who just started 3 weeks ago with B4X. Learning takes time and you have to suffer like we did. It does not happen overnight. You are using terms like: 'pulling my hair, frustrated, annoyed and many other terms used by someone who is overwhelmed. Your project has close to 25 layouts which makes one's head spin. Dude, slow down and learn one thing at a time. Follow the advice Erel gave you. and break down your project into smaller projects.
Now, let me try to address your problem with your map issue. I hope I got it right:
1. You need to move this line: records.Initialize away from Private Sub cbStudents_CheckedChange(Checked As Boolean)
2. Move the line here to this below sub:
B4X:
Private Sub lblAttendance_Click    
records.Initialize  '  move this line here
I think for now, that should get you started, but you really need to start slow.
 
Upvote 0

JonnyCav

Member
Your project is a monster. You are trying to tackle too many things at the same time. You are biting off a lot more than you can chew, especially for some one who just started 3 weeks ago with B4X. Learning takes time and you have to suffer like we did. It does not happen overnight. You are using terms like: 'pulling my hair, frustrated, annoyed and many other terms used by someone who is overwhelmed. Your project has close to 25 layouts which makes one's head spin. Dude, slow down and learn one thing at a time. Follow the advice Erel gave you. and break down your project into smaller projects.
Now, let me try to address your problem with your map issue. I hope I got it right:
1. You need to move this line: records.Initialize away from Private Sub cbStudents_CheckedChange(Checked As Boolean)
2. Move the line here to this below sub:
B4X:
Private Sub lblAttendance_Click   
records.Initialize  '  move this line here
I think for now, that should get you started, but you really need to start slow.
Noted, my friend. Thank you.
 
Upvote 0

JonnyCav

Member
I am learning, and with everything I investigate and every angle I take I'm still stumped by the single fact that my efforts are still only returning the last record.

B4X:
Private Sub btnArchiveStudent_Click
    rs=Starter.sql.ExecQuery("SELECT studentID, studentName FROM students WHERE groupName='MO02'")
    ListOfLists.Initialize
    Do While rs.NextRow
        
        Log(rs.GetString("studentID"))
        Log(rs.GetString("studentName"))
        Log(rs.RowCount)
        
    get_month_period
    
        Log(cdate)
        Log(year)
        
    Loop
    
    Dim l1 As List
    l1.Initialize

'Do While rs.NextRow
     For i = 1 To rs.RowCount
        
        
            
        l1.Add(rs.GetString("studentID"))
        l1.AddAll(Array("MO02",rs.GetString("studentName"), cdate, year, "P"))
        l1.Add(i)
        ListOfLists.Add(l1)
        rs.NextRow
        'Log(l1)
    Next

'Loop       

    
    rs.Close
    
    For Each list As List In ListOfLists
        Log(list)
    Next
End Sub

The 'LOG's of all variables are perfect.
The 'List' does one job, INCREMENTING the record number

B4X:
(ArrayList) [74, MO02, Ambe, 05-01-24, 2024, P, 1, 74, MO02, Ambe, 05-01-24, 2024, P, 2, 74, MO02, Ambe, 05-01-24, 2024, P, 3]

The same LAST entry three times but incremented at 'li.Add(i)'

What on earth is causing me me constantly overwrite the list(of list)?

I have tried so many syntaxical configurations I'm thinking of calling Guinness ;)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
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 ListOfLists
        Log(list)
    Next
End Sub
 
Upvote 0
Solution

teddybear

Well-Known Member
Licensed User
Tiny project that will automatically run and produce a debug log of what happens
In Do While Loop block, you don't add any records to ListOfLists, the rs will move to the last one. so in For Next block, the rs you get is the last record and you add it to ListOfLists.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
In Do While Loop block, you don't add any records to ListOfLists, the rs will move to the last one. so in For Next block, the rs you get is the last record and you add it to ListOfLists.
I really do not understand why you do not give the OP a chance to see the answer I gave him, because I tested his project and made the corrections he needs which solve his problem. Then if he has a follow up, we will go from there. There is always a tendency in this forum for members to bump other answers quickly without giving a chance to new members to absorb the prior answers, unless of course there is an absolute reason for bumping.
 
Upvote 0

JonnyCav

Member
Is my logic wrong?
Each record in the resultset has different values
The Loop scrolls through the rows, assigns the values to the variables, ADDS this 'row' to the list and repeats
adding new values until the end is reached.
In this instance I'm expecting a List to look like
[72, MO02, Vanessa, 05-01-24, 2024, P, 1]
[73, MO02, Millicent, 05-01-24, 2024, P, 2]
[74, MO02, Ambe, 05-01-24, 2024, P, 3]

(As per Erel's listoflists example (only his example has static values(2,3,4)) )
 
Upvote 0
Top