B4A Library [Class] Flexible Table

Kanne

Member
Licensed User
Longtime User
I get "ArrayIndexOutOfBoundsException" when pushing "hide column 4" (because default table only has 2 cols ?) but can't test by changing table via "Change Table" because this also get's an out of bound.
Disabling
"Table1.SetCellAlignments(Alignments)"
will load the table but scrolling crashes with
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[])' on a null object reference
 

klaus

Expert
Licensed User
Longtime User
How and when do you get this.
It's difficult for me to find out what happens and why without knowing how you get it.
I get "ArrayIndexOutOfBoundsException" when pushing "hide column 4" (because default table only has 2 cols ?)
Which default table are you speaking of?
but can't test by changing table via "Change Table" because this also get's an out of bound.
What is "Change Table" ?
 

Kanne

Member
Licensed User
Longtime User
I'm sorry for not having described it in detail: the new table.bas is running fine in my project, but your example app which you provided crashes by nearly doing anything.
When starting your app I can see two tables (first one with only 2 cols) and a button for hiding col 4 ! Another button labeled "change table" also crashes ...
so I am speaking about your app, not mine
 

martha

Member
If the header's height is changed and a column is fixed, the fixed panel has an incorrect height.

B4X:
Table1.HeaderHeight = nHeight
Table1.NumberOfFixedColumns = 1
 

Attachments

  • table.png
    25.9 KB · Views: 188

coldtech

Member
Licensed User
Longtime User
Tried the demo for 3.17 and it threw an exception in this sub routine until I checked if Row was not -1. I was just playing with it and hit the button to see what it did.
If I actually made a selection the error does not occur.

B4X:
'removes the specific color of the given row
Public Sub RemoveRowColorN(Row As Int)
    If Row > 0 Then      
        lstRowColorIndexes.Set(Row, 0)
        If pnlTable.IsInitialized Then
            RefreshLabels
        End If
    End If
End Sub
 

klaus

Expert
Licensed User
Longtime User
Thank you for reporting this.

I should have tested it in the demo program code like this:
B4X:
Sub btnRemoveColor_Click
    If SelectedRow1 > -1 Then
        Table1.UnselectRow(SelectedRow1)
        Table1.RemoveRowColorN(SelectedRow1)
    End If
End Sub

But, I will add a check in the Class code to avoid any trouble for the next update.
 
Last edited:

coldtech

Member
Licensed User
Longtime User
Run the demo and immediately hit the Remove Color button.

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 2350 (Table)
java.lang.ArrayIndexOutOfBoundsException: length=22; index=-1
at java.util.ArrayList.set(ArrayList.java:455)
 

klaus

Expert
Licensed User
Longtime User
Run the demo and immediately hit the Remove Color button.
That's what I did, and, as written in my previous post, I have added a check in the class code that the row index is not out of bounds for the next update, not yet published.
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Yes, I know. If I remember well I looked at your code and it used other modules and I found it complicated to include it in the class.
I was afraid do introduce more bugs than adding a new feature.

I have sorted this now all and changed the sort from being based on a list holding types to an index Quicksort.
This is making it both more simple (less code) and also faster.
Fully tested now (but only on a Samsung S10) and all working fine.

In case somebody is interested this is all the Quicksort code:

B4X:
Sub ReDimPreserveInt(arrInt() As Int, lSize As Int) As Int()
 Dim i As Long
 Dim arrInt2(lSize) As Int
 For i = 0 To arrInt.Length - 1
arrInt2(i) = arrInt(i)
Next

 Return arrInt2
End Sub

Sub QSort1DString_IDX(arrString() As String, _
bAscending As Boolean, _
                      bCompareCaseInsensitive As Boolean) As Int()
 Dim i As Int
Dim j As Int
Dim Lo As Int
Dim Hi As Int
Dim StPtr As Int
Dim HighIndex As Int
Dim strCmp As String
Dim lTmp As Int
Dim arrIndex() As Int

HighIndex = arrString.Length - 1

Dim StLo() As Int
Dim StHi() As Int
 Dim StSize As Int
 If StSize = 0 Then
StSize = 255
Dim StLo(StSize) As Int
Dim StHi(StSize) As Int
 End If
 Dim arrIndex(HighIndex + 1) As Int
 'load the index array
'--------------------
For i = 0 To HighIndex
arrIndex(i) = i
 Next
 StLo(0) = 0
StHi(0) = HighIndex
StPtr = 1

If bAscending Then
  If bCompareCaseInsensitive Then
   Do While StPtr > 0
StPtr = StPtr - 1
Lo = StLo(StPtr)
    Hi = StHi(StPtr)
    'QuickSort
'-----------------
Do While Lo < Hi
i = Lo
     j = Hi
     strCmp = arrString(arrIndex(CInt(Lo + Hi) / 2)).ToLowerCase
     Do  While i <= j
Do While arrString(arrIndex(i)).ToLowerCase.CompareTo(strCmp) < 0
i = i + 1
Loop
Do While arrString(arrIndex(j)).ToLowerCase.CompareTo(strCmp) > 0
j = j - 1
Loop
If i <= j Then
lTmp = arrIndex(i)
arrIndex(i) = arrIndex(j)
arrIndex(j) = lTmp
i = i + 1
j = j - 1
End If 'If i <= j
     Loop
     If j - Lo < Hi - i Then
If i < Hi Then
StLo(StPtr) = i
StHi(StPtr) = Hi
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
'as B4A doesn't do ReDim Preserve
'--------------------------------
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Hi = j
Else 'If j - Lo < Hi - i
If Lo < j Then
StLo(StPtr) = Lo
StHi(StPtr) = j
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Lo = i
End If 'If j - Lo < Hi - i
Loop
Loop
Else 'If bCompareCaseInsensitive
Do While StPtr > 0
StPtr = StPtr - 1
Lo = StLo(StPtr)
    Hi = StHi(StPtr)
    Do While Lo < Hi
i = Lo
j = Hi
     strCmp = arrString(CInt(arrIndex((Lo + Hi) / 2)))
     Do While i <= j
Do While arrString(arrIndex(i)).CompareTo(strCmp) < 0
i = i + 1
Loop
Do While arrString(arrIndex(j)).CompareTo(strCmp) > 0
j = j - 1
Loop
If i <= j Then
lTmp = arrIndex(i)
arrIndex(i) = arrIndex(j)
arrIndex(j) = lTmp
i = i + 1
j = j - 1
End If 'If i <= j
     Loop
     If j - Lo < Hi - i Then
If i < Hi Then
StLo(StPtr) = i
StHi(StPtr) = Hi
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Hi = j
Else 'If j - Lo < Hi - i
If Lo < j Then
StLo(StPtr) = Lo
StHi(StPtr) = j
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Lo = i
End If 'If j - Lo < Hi - i
Loop
Loop
End If 'If bCompareCaseInsensitive
Else 'If bAscending
  If bCompareCaseInsensitive Then
   Do While StPtr > 0
StPtr = StPtr - 1
Lo = StLo(StPtr)
    Hi = StHi(StPtr)
    'QuickSort
'-----------------
Do While Lo < Hi
i = Lo
     j = Hi
     strCmp = arrString(arrIndex(CInt(Lo + Hi) / 2)).ToLowerCase
     Do  While i <= j
Do While arrString(arrIndex(i)).ToLowerCase.CompareTo(strCmp) > 0
i = i + 1
Loop
Do While arrString(arrIndex(j)).ToLowerCase.CompareTo(strCmp) < 0
j = j - 1
Loop
If i <= j Then
lTmp = arrIndex(i)
arrIndex(i) = arrIndex(j)
arrIndex(j) = lTmp
i = i + 1
j = j - 1
End If 'If i <= j
     Loop
     If j - Lo < Hi - i Then
If i < Hi Then
StLo(StPtr) = i
StHi(StPtr) = Hi
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
'as B4A doesn't do ReDim Preserve
'--------------------------------
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Hi = j
Else 'If j - Lo < Hi - i
If Lo < j Then
StLo(StPtr) = Lo
StHi(StPtr) = j
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Lo = i
End If 'If j - Lo < Hi - i
Loop
Loop
Else 'If bCompareCaseInsensitive
Do While StPtr > 0
StPtr = StPtr - 1
Lo = StLo(StPtr)
    Hi = StHi(StPtr)
    Do While Lo < Hi
i = Lo
j = Hi
     strCmp = arrString(CInt(arrIndex((Lo + Hi) / 2)))
     Do While i <= j
Do While arrString(arrIndex(i)).CompareTo(strCmp) > 0
i = i + 1
Loop
Do While arrString(arrIndex(j)).CompareTo(strCmp) < 0
j = j - 1
Loop
If i <= j Then
lTmp = arrIndex(i)
arrIndex(i) = arrIndex(j)
arrIndex(j) = lTmp
i = i + 1
j = j - 1
End If 'If i <= j
     Loop
     If j - Lo < Hi - i Then
If i < Hi Then
StLo(StPtr) = i
StHi(StPtr) = Hi
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Hi = j
Else 'If j - Lo < Hi - i
If Lo < j Then
StLo(StPtr) = Lo
StHi(StPtr) = j
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Lo = i
End If 'If j - Lo < Hi - i
Loop
Loop
End If 'If bCompareCaseInsensitive
 End If    'If bAscending
 Return arrIndex
End Sub

Sub QSort1DInt_IDX(arrInt() As Int, _
       bAscending As Boolean) As Int()
 Dim i As Int
Dim j As Int
Dim Lo As Int
Dim Hi As Int
Dim StPtr As Int
Dim HighIndex As Int
Dim iCmp As Int
Dim lTmp As Int
Dim arrIndex() As Int

HighIndex = arrInt.Length - 1

Dim StLo() As Int
Dim StHi() As Int
 Dim StSize As Int
 If StSize = 0 Then
StSize = 255
Dim StLo(StSize) As Int
Dim StHi(StSize) As Int
 End If
 Dim arrIndex(HighIndex + 1) As Int
 'load the index array
'--------------------
For i = 0 To HighIndex
arrIndex(i) = i
 Next
 StLo(0) = 0
StHi(0) = HighIndex
 StPtr = 1
 If bAscending Then
Do While StPtr > 0
StPtr = StPtr - 1
Lo = StLo(StPtr)
   Hi = StHi(StPtr)
   'QuickSort
'-----------------
Do While Lo < Hi
i = Lo
    j = Hi
    iCmp = arrInt(arrIndex(CInt(Lo + Hi) / 2))
    Do  While i <= j
Do While arrInt(arrIndex(i)) < iCmp
i = i + 1
Loop
Do While arrInt(arrIndex(j)) > iCmp
j = j - 1
Loop
If i <= j Then
lTmp = arrIndex(i)
arrIndex(i) = arrIndex(j)
arrIndex(j) = lTmp
i = i + 1
j = j - 1
End If 'If i <= j
    Loop
    If j - Lo < Hi - i Then
If i < Hi Then
StLo(StPtr) = i
StHi(StPtr) = Hi
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
'as B4A doesn't do ReDim Preserve
'--------------------------------
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Hi = j
Else 'If j - Lo < Hi - i
If Lo < j Then
StLo(StPtr) = Lo
StHi(StPtr) = j
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Lo = i
End If 'If j - Lo < Hi - i
Loop
Loop
Else 'If bAscending
Do While StPtr > 0
StPtr = StPtr - 1
Lo = StLo(StPtr)
   Hi = StHi(StPtr)
   'QuickSort
'-----------------
Do While Lo < Hi
i = Lo
    j = Hi
    iCmp = arrInt(arrIndex(CInt(Lo + Hi) / 2))
    Do  While i <= j
Do While arrInt(arrIndex(i)) > iCmp
i = i + 1
Loop
Do While arrInt(arrIndex(j)) < iCmp
j = j - 1
Loop
If i <= j Then
lTmp = arrIndex(i)
arrIndex(i) = arrIndex(j)
arrIndex(j) = lTmp
i = i + 1
j = j - 1
End If 'If i <= j
    Loop
    If j - Lo < Hi - i Then
If i < Hi Then
StLo(StPtr) = i
StHi(StPtr) = Hi
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
'as B4A doesn't do ReDim Preserve
'--------------------------------
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Hi = j
Else 'If j - Lo < Hi - i
If Lo < j Then
StLo(StPtr) = Lo
StHi(StPtr) = j
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Lo = i
End If 'If j - Lo < Hi - i
Loop
Loop
 End If 'If bAscending
 Return arrIndex
End Sub

Sub QSort1DDouble_IDX(arrDouble() As Double, _
       bAscending As Boolean) As Int()
 Dim i As Int
Dim j As Int
Dim Lo As Int
Dim Hi As Int
Dim StPtr As Int
Dim HighIndex As Int
Dim dCmp As Double
Dim lTmp As Int
Dim arrIndex() As Int

HighIndex = arrDouble.Length - 1

Dim StLo() As Int
Dim StHi() As Int
 Dim StSize As Int
 If StSize = 0 Then
StSize = 255
Dim StLo(StSize) As Int
Dim StHi(StSize) As Int
 End If
 Dim arrIndex(HighIndex + 1) As Int
 'load the index array
'--------------------
For i = 0 To HighIndex
arrIndex(i) = i
 Next
 StLo(0) = 0
StHi(0) = HighIndex
StPtr = 1

If bAscending Then
Do While StPtr > 0
StPtr = StPtr - 1
Lo = StLo(StPtr)
   Hi = StHi(StPtr)
   'QuickSort
'-----------------
Do While Lo < Hi
i = Lo
    j = Hi
    dCmp = arrDouble(arrIndex(CInt(Lo + Hi) / 2))
    Do  While i <= j
Do While arrDouble(arrIndex(i)) < dCmp
i = i + 1
Loop
Do While arrDouble(arrIndex(j)) > dCmp
j = j - 1
Loop
If i <= j Then
lTmp = arrIndex(i)
arrIndex(i) = arrIndex(j)
arrIndex(j) = lTmp
i = i + 1
j = j - 1
End If 'If i <= j
    Loop
    If j - Lo < Hi - i Then
If i < Hi Then
StLo(StPtr) = i
StHi(StPtr) = Hi
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
'as B4A doesn't do ReDim Preserve
'--------------------------------
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Hi = j
Else 'If j - Lo < Hi - i
If Lo < j Then
StLo(StPtr) = Lo
StHi(StPtr) = j
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Lo = i
End If 'If j - Lo < Hi - i
Loop
Loop
Else
Do While StPtr > 0
StPtr = StPtr - 1
Lo = StLo(StPtr)
   Hi = StHi(StPtr)
   'QuickSort
'-----------------
Do While Lo < Hi
i = Lo
    j = Hi
    dCmp = arrDouble(arrIndex(CInt(Lo + Hi) / 2))
    Do  While i <= j
Do While arrDouble(arrIndex(i)) > dCmp
i = i + 1
Loop
Do While arrDouble(arrIndex(j)) < dCmp
j = j - 1
Loop
If i <= j Then
lTmp = arrIndex(i)
arrIndex(i) = arrIndex(j)
arrIndex(j) = lTmp
i = i + 1
j = j - 1
End If 'If i <= j
    Loop
    If j - Lo < Hi - i Then
If i < Hi Then
StLo(StPtr) = i
StHi(StPtr) = Hi
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
'as B4A doesn't do ReDim Preserve
'--------------------------------
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Hi = j
Else 'If j - Lo < Hi - i
If Lo < j Then
StLo(StPtr) = Lo
StHi(StPtr) = j
StPtr = StPtr + 1
If StPtr >= StSize Then 'never seen this happen yet
StSize = StSize + StSize
StLo = ReDimPreserveInt(StLo, StSize)
StHi = ReDimPreserveInt(StHi, StSize)
End If
End If
Lo = i
End If 'If j - Lo < Hi - i
Loop
Loop
 End If
 Return arrIndex
End Sub

RBS
 

klaus

Expert
Licensed User
Longtime User
I had a look at your code.
About 550 lines of code, that's a lot.
What exactly does the function CInt?
I saw in the forum HERE that you use this code:
B4X:
Sub CInt(o As Object) As Int
    Return Floor(o)
End Sub
Is this what you are using?
If Yes, why not using directly Floor(), you are only using integers in the function?

And you use this.
strCmp = arrString(arrIndex(CInt(Lo + Hi) / 2)).ToLowerCase
and sometimes this:
strCmp = arrString(CInt(arrIndex((Lo + Hi) / 2)))
arrIndex(CInt(Lo + Hi) / 2)
versus CInt(arrIndex((Lo + Hi) / 2))
is this really what you need?
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User

Yes, 550 lines is a lot code, but these are general purpose functions and not really part of the flexible table code.
Also I used these functions for a long time in VBA and VB6 and I know they work well. Just now converted them to
B4A code.

About CInt: Yes, I think you are right there, and don't need the function call. In my VBA/VB6 code I have integer division: \
and B4A doesn't have that, so quickly picked this function. In fact, thinking about it, I may just do normal division as in
B4A I think if you int var = int var / int var you will get an int value in any case, so do away with CInt etc. Will have a look at this and fix it.
Thanks for bringing that up.

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User

Had a look at the CInt thing and noticed many weren't doing anything due to the brackets: CInt(Lo + Hi).
Even the ones that were doing something I don't need, so they are all gone from the Quicksorts and this speeded
it all up a bit further.

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User

I had a look at doing the Quicksort directly on the ResultSet, saving setting up the sort arrays.
It is slower than the array sort, but it will make the code a bit simpler.

Secondly, of course the Quicksort code can be halved as we don't need to do bAscending True or False.
As this is an index sort we can always sort ascending and reverse the data update if the sort was descending:

B4X:
 If bAscending Then
  For i = 0 To lUB
   Data.Set(i, DataTemp.Get(arrSortIndex(i)))
  Next
 Else
  For i = 0 To lUB
   Data.Set(i, DataTemp.Get(arrSortIndex(lUB - i)))
  Next
 End If

Thirdly, we could keep the index sorts per column (stored in a map, with the column number as key?) so if we
sort the same column again we can reuse that index and save another sort. As this sort is so fast, not sure this is worth it.

RBS
 

Kanne

Member
Licensed User
Longtime User
Hi Klaus,
is it somehow possible to manually set color for single cell, e.g. by something like
B4X:
'JK - Test
Public Sub SetCellColor(Row As Int, Col As Int, Color As Int) 
    Dim lbls() As Label
    lbls = visibleRows.get(Row)
    If lbls = Null Then 
        Log("Row" & Row & " not in visible Rows !")
        Return
    End If
    lbls(Col).TextColor = Color
    visibleRows.Put(Row,lbls)
End Sub

When using this by
B4X:
    For j = 0 To Table1.NumberOfRows-1
        For i = 1 To 5
            Table1.SetCellColor(j,i+1,Colors.Red)
        Next
    Next
I get some cells (maybe cached or at that time visible) updated, but when scrolling the table new cells are in old color.
 
Last edited:

klaus

Expert
Licensed User
Longtime User
is it somehow possible to manually set color for single cell, e.g. by something like
Sorry no.
I know, this has been asked for several times, but this would need to memorize the color for each cell.
The class is based on 'lazy loading', that means that rows are dynamically removed and redrawn depending on the scrolling.
Currently, the content of each cell is memorized, that's normal, but for the colors we would need to 'double' the allowed memory.
And I am afraid that this will slow down the scrolling behaviour, because for each cell update we need to set it's color.
For small sizes, no problem, but for big sizes???
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…