Android Question SQLLiteLight2 example SOLVED

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

The SQLLiteLight2 example has a bug and as much as I go over it, Klaus' code appears all good.
The error occurs when I try to delete the most recent record. A few details below:

B4X:
Private Sub DeleteEntry
    Private Query As String
    Private Answ As Int
   
    'ask the user for confirmation
    Answ = Msgbox2("Do you really want to delete " & edtFirstName.Text & " " & edtLastName.Text, "Delete entry", "Yes", "", "No", Null)

    If Answ = DialogResponse.POSITIVE Then            'if yes, delete the entry
        Query = "DELETE FROM persons WHERE rowid = " & Main.RowIDList.Get(Main.CurrentIndex)
        Main.SQL1.ExecNonQuery(Query)                                    'delete the entry
        Main.RowIDList.RemoveAt(Main.CurrentIndex)                            'remove the rowid from the list
        If Main.CurrentIndex = Main.RowIDList.Size - 1 Then            'if the current index is the last one
            Main.CurrentIndex = Main.CurrentIndex - 1                    'decrement it by 1
        End If
        ShowEntry(Main.CurrentIndex)                                        'show the next entry
        ToastMessageShow("Entry deleted", False)    'confirmation for the user
        ShowButtons
    End If
End Sub


Private Sub ShowEntry(EntryIndex As Int)
    Private RowID As Int
   
    If Main.RowIDList.Size = 0 Then             'check if database is empty
        Return                                                    'if the database is empty leave the routine
    End If
   
    If Mode = "Add" Then
        lblRowID.Text = ""
        edtFirstName.Text = ""
        edtLastName.Text = ""
        edtCity.Text = ""
    Else
        Private ResultSet1 As ResultSet
        RowID = Main.RowIDList.Get(EntryIndex)        'get the rowid for the given entry index
        'read the entry with the given ID
        ResultSet1 = Main.SQL1.ExecQuery("SELECT * FROM persons WHERE rowid = " & RowID)
        lblRowID.Text = RowID                                                                    'display the ID
        ResultSet1.NextRow
        edtFirstName.Text = ResultSet1.GetString("FirstName")        'read the value of the FirstName column
        edtLastName.Text = ResultSet1.GetString("LastName")            'read the value of the LasstName column
        edtCity.Text = ResultSet1.GetString("City")                            'read the value of the City column
        ResultSet1.Close                                                                                'close the ResultSet, we don't need it anymore
    End If
    ShowButtons
End Sub

The error logs as this line:
RowID = Main.RowIDList.Get(EntryIndex) 'get the rowid for the given entry index
in the Private Sub ShowEntry(EntryIndex As Int).

The Log in part:
** Activity (edit) Create, isFirst = false **
** Activity (edit) Resume **
edit_showentry (B4A line: 45)
RowID = Main.RowIDList.Get(EntryIndex) 'get the
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.get(ArrayList.java:437)
at anywheresoftware.b4a.objects.collections.List.Get(List.java:117)
at b4a.sqlitelight2.edit._showentry(edit.java:894)
at b4a.sqlitelight2.edit._deleteentry(edit.java:647)
at b4a.sqlitelight2.edit._btndelete_click(edit.java:516)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)

Obviously the RowID goes out of range but i can't fault it.

Any response welcome.

Regards Roger
 

Jorge M A

Well-Known Member
Licensed User
The small problem is in the line where that compares the current index with the size of the list by subtracting 1.
Since the previous line has already been removed from the list of ID's and the size has already been decreased.
B4X:
        Main.SQL1.ExecNonQuery(Query)                                    'delete the entry
        Main.RowIDList.RemoveAt(Main.CurrentIndex)                            'remove the rowid from the list
        If Main.CurrentIndex = Main.RowIDList.Size - 1 Then            'if the current index is the last one
            Main.CurrentIndex = Main.CurrentIndex - 1                    'decrement it by 1
        End If

It is enough to remove the subtraction of 1.

B4X:
        Main.RowIDList.RemoveAt(Main.CurrentIndex)                            'remove the rowid from the list
        'If Main.CurrentIndex = Main.RowIDList.Size - 1 Then            '<<<--- WRONG SIZE if the current index is the last one
        If Main.CurrentIndex = Main.RowIDList.Size  Then            'if the current index is the last one
            Main.CurrentIndex = Main.CurrentIndex - 1                    'decrement it by 1
        End If
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
The small problem is in the line where that compares the current index with the size of the list by subtracting 1.
Since the previous line has already been removed from the list of ID's and the size has already been decreased.
B4X:
        Main.SQL1.ExecNonQuery(Query)                                    'delete the entry
        Main.RowIDList.RemoveAt(Main.CurrentIndex)                            'remove the rowid from the list
        If Main.CurrentIndex = Main.RowIDList.Size - 1 Then            'if the current index is the last one
            Main.CurrentIndex = Main.CurrentIndex - 1                    'decrement it by 1
        End If

It is enough to remove the subtraction of 1.

B4X:
        Main.RowIDList.RemoveAt(Main.CurrentIndex)                            'remove the rowid from the list
        'If Main.CurrentIndex = Main.RowIDList.Size - 1 Then            '<<<--- WRONG SIZE if the current index is the last one
        If Main.CurrentIndex = Main.RowIDList.Size  Then            'if the current index is the last one
            Main.CurrentIndex = Main.CurrentIndex - 1                    'decrement it by 1
        End If


Many thanks Jorge M A. This is something I would probably never found, on the plus side it is another step in learning.

Roger
 
Upvote 0
Top