Table Class: Delete row, Show row index error

Bryan

Member
Licensed User
Longtime User
I'm using the Table class which is working very well up to the point that I wanted to add to the table class ability to delete a row. I have found an example on this forum which works but only if you don't try to delete the entry(last row) of the table. If you do, you get an "IndexOutOfBoundsException" in the "ShowRow" sub in the table class. Is there some way to get around this. I've tried a few things but am having no luck.

My generated error trying to delete the last line in my table.

B4X:
table_showrow (B4A line: 181)
values = Data.Get(row)
java.lang.IndexOutOfBoundsException: Invalid index 21, size is 21
   at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
   at java.util.ArrayList.get(ArrayList.java:311)
   at anywheresoftware.b4a.objects.collections.List.Get(List.java:108)
   at anywheresoftware.b4a.table.table._showrow(table.java:1333)
   at anywheresoftware.b4a.table.table._selectrow(table.java:1037)
   at anywheresoftware.b4a.table.table._deleterow(table.java:400)
   at anywheresoftware.b4a.table.main._del_click(main.java:470)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:155)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:151)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:59)
   at android.view.View.performClick(View.java:2538)
   at android.view.View$PerformClick.run(View.java:9152)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3687)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.IndexOutOfBoundsException: Invalid index 21, size is 21
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **

This is the DeleteRow sub added to the table class

B4X:
Public Sub DeleteRow    
Dim Row As Int    
Row = SelectedRow    
Data.RemoveAt(Row)    
Dim lastRow As Int    
lastRow = Data.Size - 1
For i = lastRow To 0 Step -1        
If lastRow < (SV.ScrollPosition + SV.Height) / RowHeight + 1 Then            
ShowRow(lastRow)                
End If        
SelectRow(lastRow)        
SV.Panel.Height = Data.Size * RowHeight        
lastRow = lastRow -1
Log(lastRow)
Next    
SV.Panel.Height = Data.Size * RowHeight    
End Sub

This is the ShowRow sub in table class

B4X:
Private Sub ShowRow(row As Int)
   If visibleRows.ContainsKey(row) Then Return
   Log("ShowRow: " & row)
   Dim lbls() As Label
   Dim values() As String
   lbls = GetLabels(row)
   values = Data.Get(row)
   visibleRows.Put(row, lbls)
   Dim rowColor() As Object
   If row = SelectedRow Then
      rowColor = SelectedDrawable
   Else If row Mod 2 = 0 Then
      rowColor = Drawable1
   Else
      rowColor = Drawable2
   End If
   For I = 0 To lbls.Length - 1
      SV.Panel.AddView(lbls(I), Header.GetView(I).Left, row * RowHeight, Header.GetView(I).Width, _
         RowHeight - 1dip)
      lbls(I).Text = values(I)
      lbls(I).Background = rowColor(I)
   Next
End Sub

Any help is greatly appreciated.
Thanks,
Bryan
 

Bryan

Member
Licensed User
Longtime User
OK, I fixed my own problem. Here is the modified "ShowRow" sub in the table class that will allow you to delete the last row of your table without giving an Index out of bounds error.

B4X:
Private Sub ShowRow(row As Int)
   If visibleRows.ContainsKey(row) Then Return
   If Data.Size - 1 < row Then     'newly added line
   row = Data.Size - 1                'newly added line
   End If                                    'newly added line 
   Log("ShowRow: " & row)
   Dim lbls() As Label
   Dim values() As String
   lbls = GetLabels(row)
   values = Data.Get(row)
   visibleRows.Put(row, lbls)
   Dim rowColor() As Object
   If row = SelectedRow Then
      rowColor = SelectedDrawable
   Else If row Mod 2 = 0 Then
      rowColor = Drawable1
   Else
      rowColor = Drawable2
   End If
   For I = 0 To lbls.Length - 1
      SV.Panel.AddView(lbls(I), Header.GetView(I).Left, row * RowHeight, Header.GetView(I).Width, _
         RowHeight - 1dip)
      lbls(I).Text = values(I)
      lbls(I).Background = rowColor(I)
   Next
End Sub
 
Upvote 0

Eumel

Active Member
Licensed User
Longtime User
Danke Bryan,

der Fehler war grad bei mir aufgetaucht. Mit deiner Lösung funkt die App wieder :D
 
Upvote 0
Top