Android Question Pass list of objects from class module to main module

Lahksman

Active Member
Licensed User
Longtime User
Hi

I'm having some trouble with passing my list of objects from my datamanager class to my main module.
While i'm still in my datamanager i have no problem reading my list, but when i pass this list to my main module it becomes unreadable.

Code from my datamanager.
B4X:
Sub GetClubs
    Dim cursor1 As Cursor
    Dim row As Int
    Dim objClub As clsClub
    Dim arClub As List : arClub.Initialize
   
    Try
        cursor1 = Main.sql1.ExecQuery("SELECT * FROM clubs ORDER BY clubNaam ASC")
        If cursor1.RowCount > 0 Then
            intRowNumber = cursor1.RowCount
            For row = 0 To intRowNumber -1
                cursor1.Position = row
                objClub.ClubID = cursor1.GetInt("clubID")
                objClub.ClubNaam = cursor1.GetString("clubNaam")
                objClub.ClubGPS = cursor1.GetString("clubGPS")
                arClub.Add(objClub)       
            Next
        End If
    Catch
        Log(LastException.Message)
        ToastMessageShow(LastException.Message, True)
    End Try
    Return arClub
End Sub

Code from my Main module
B4X:
Dim arClubs As List : arClubs.Initialize
    arClubs = dmDataManager.GetClubs

    Try
        If arClubs.Size > 0 Then
            For i = 0 To arClubs.Size -1
                Dim objClub As clsClub = arClubs.Get(i)
                spnClub.Add(objClub.ClubNaam)
            Next
        Else
            spnClub.Enabled = False
        End If
    Catch
        Log(LastException.Message)
        ToastMessageShow(LastException.Message, True)
    End Try
 

DonManfred

Expert
Licensed User
Longtime User
I dont know much about sqlite on android/b4a
BUT
In a b4a app without database you are adding a reference to the same (ONE) objectinstance of objClub

B4X:
 arClub.Add(objClub)

This can cause that all entries in arClub will have the same values. In fact; the data from the last cursor-position

Usually i would write it like this

B4X:
Sub GetClubs
    Dim cursor1 As Cursor
    Dim row As Int
    Dim arClub As List : arClub.Initialize

    Try
        cursor1 = Main.sql1.ExecQuery("SELECT * FROM clubs ORDER BY clubNaam ASC")
        If cursor1.RowCount > 0 Then
            intRowNumber = cursor1.RowCount
            For row = 0 To intRowNumber -1
                cursor1.Position = row
                Dim objClub As clsClub ' DIMming here to create a new instance of objClub on each iteration
                objClub.ClubID = cursor1.GetInt("clubID")
                objClub.ClubNaam = cursor1.GetString("clubNaam")
                objClub.ClubGPS = cursor1.GetString("clubGPS")
                arClub.Add(objClub)    
            Next
        End If
    Catch
        Log(LastException.Message)
        ToastMessageShow(LastException.Message, True)
    End Try
    Return arClub
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…