Android Question Displaying an SQL database in a list

Colin Evans

Active Member
Licensed User
Longtime User
Hi, just wondered if it was possible and if anyone could give me a nudge in the right direction.

I have a database, table that contains a years worth of data, i.e. 365 rows each row having four fields,
Date, Time, Info, Name
I want to load the full list of data onto the screen into a scrolling list but also highlight today's date in the list nad then bea ble to scroll up or down the list, but then have a button to return me back to today's date entry in the list

Firstly is this possible and if so any idea's where I should start
 

klaus

Expert
Licensed User
Longtime User
Maybe this code works, not tested.
DefaultImage is a default bitmap if there is no image.
B4X:
Do While ResultSet1.NextRow
   Private bmp As Bitmap
    If ResultSet1.GetBlob2(4) = Null Then
        bmp = DefaultImage
    Else
        Private Buffer() As Byte 'declare an empty byte array
        Buffer = ResultSet1.GetBlob2(4)
        Private InputStream1 As InputStream
        InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)       
        bmp.Initialize2(InputStream1)
        InputStream1.Close
    End If
    CLV1.Add(CreateItem(CLV1.AsView.Width, ResultSet1.GetString2(0), ResultSet1.GetString2(1), ResultSet1.GetString2(2), ResultSet1.GetString2(3), bmp), ResultSet1.GetString2(0))
Loop
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Sorry Klaus get the following error
java.lang.RuntimeException: Error loading bitmap.

I amended the code to have the default image allocated to a file
B4X:
Private Sub FillCLV
    Dim defaultImage As Bitmap
    defaultImage.Initialize(File.DirAssets,"ditl.jpg")
    Private ResultSet1 As ResultSet
    Private Query As String
    Query = "SELECT ID, Fish, date, location, image FROM fishcaught"
    ResultSet1 = Starter.SQL1.ExecQuery(Query)
    Do While ResultSet1.NextRow
        Private bmp As Bitmap
        If ResultSet1.GetBlob2(4) = Null Then
        bmp = defaultImage
        Else
        Private Buffer() As Byte 'declare an empty byte array
            Buffer = ResultSet1.GetBlob2(4)
            Private InputStream1 As InputStream
            InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
            Private bmp As Bitmap
            bmp.Initialize2(InputStream1)
            InputStream1.Close
            End If
        CLV1.Add(CreateItem(CLV1.AsView.Width, ResultSet1.GetString2(0), ResultSet1.GetString2(1), ResultSet1.GetString2(2), ResultSet1.GetString2(3), bmp), ResultSet1.GetString2(0))
    Loop
    ResultSet1.Close
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I amended the code to have the default image allocated to a file
I expected you to do something like what you did with this sentence: DefaultImage is a default bitmap if there is no image :).

You should put the bitmap as a Global variable and initialize it in Activity_Create to avoid reading the bitmap every time you fill the CustomListView
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
1. You have been getting an enormous amount of help in this thread from @klaus. The least you can do is give him some 'like' on some of his posts.
2. This is a very interesting thread you posted. A more appropriate title for it would have been perhaps something like: Displaying a SQLite Table Using xCustomListView. It would make it a lot easier for members to locate it when searching for a similar topic.
3. Although @klaus was instrumental in guiding you through this thread, I think it is always more appropriate to address your posts to the general community rather than one individual. By addressing one member, you are eliminating another 96000 members that can potentially help.
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
I apologize if I'm not following protocol, I am very appreciative of everyone who as helped me in this posting and others especially Klaus and Erel, wasn't aware of the 'liking' aspect to boost ratings or whatever but as I said I am and have always stated that I appreciate any assistance given.

It's now a shame that I feel I won't be able to use the forum given I'm a 62 year old novice trying to keep my brain active and trying to learn how to program for my own personal use, in case I step out of line

Thanks again to all who have helped
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Just a quick update, the problem with the error java.lang.RuntimeException: Error loading bitmap. was the way it was setting the Blob to Null in the database, looks like it was entering a null string value 'null' because when I edited the database in DB Browser for SQL, removed the 'null' and set to 'Null' the module worked.
Thanks again
 
Upvote 0
Top