B4J Question Why does tableview selectrow always select the last row

raphipps2002

Active Member
Licensed User
Longtime User
Thanks to member 'Daestrum' I have written some code which simply looks at each row in a tableview then looks at cell(1) (a date field) to see if it falls between date A and B. If so then that row number is stored in a list. After identification of rows meeting the criteria (between date A and B), I then use a javaobject to actually highlight those rows. However, my problem is the last row is always highlighted whether the criteria is met or not. And cannot see why. Row 0 is always excluded hence the loop runs from row 1. I can fudge this problem by using tbl.selectrow = 0 everytime the loop cycles. But it is not ideal as there may be occasions when I can't use the fudge. Can any one shed some light on this. Daestrum kindly showed me some js nashorn but i'd rather stick to the javaobject and crack this error.



B4X:
Try
        Dim jo1 As JavaObject = dtprangeFrom
        Dim jo2 As JavaObject = dtpRangeTo
        Dim sDate1 As String = jo1.RunMethod("getValue", Null)
        Dim dDate1 As Long = DateTime.DateParse(sDate1)
        Dim sDate2 As String = jo2.RunMethod("getValue", Null)
        Dim dDate2 As Long = DateTime.DateParse(sDate2)
    Catch
        msg.Show("Select date range","Date range error")
        Log(LastException)   
        Return
    End Try
   
    Log("Period from " & DateTime.Date(dDate1) & " to " & DateTime.Date(dDate2))
       
    'select row
    Dim jo As JavaObject = tblCB
    Dim sm As JavaObject
    sm = jo.RunMethod("getSelectionModel",Null)
    Dim sm1 As JavaObject
    sm1.InitializeStatic("javafx.scene.control.SelectionMode")
    sm.RunMethod("setSelectionMode",Array(sm1.GetField("MULTIPLE")))
   
    Dim Li As List
    Li.Initialize()

    For i = 1 To tblCB.Items.Size-1
       
        tblCB.SelectedRow = i
        Dim row() As Object = tblCB.SelectedRowValues
        Dim DT As Long = DateTime.DateParse(row(1))
   
        Log("Transaction selected is " & row(0) & " " & row(1) & " and long value is " & DT)
       
        If DT >= dDate1 And DT <=dDate2 Then
            'Now select the actual row
            Li.Add(i)   
            Log(i & " is in date range" & DateTime.Date(DT))
        End If   
   
        tblCB.SelectedRow = 0
       
   
    Next
   
    For Each j In Li
        Log("selecting item row " & j)
        jo.RunMethodJO("getSelectionModel",Null).RunMethod("select",Array(j))
    Next

Interestingly the last row highlighted IS NOT selected however when I test the rows selected using this code which will be confusing to a user

B4X:
    'Get selected rows from the table as list
    Dim l As List
    l.Initialize
    l.AddAll(sm.RunMethodJO("getSelectedItems",Null))
   
    'Display the data in the rows
     Dim ss As String = ""
    For Each o() As Object In l
        For Each o1 In o
            If ss = "" Then
                ss=01
            Else
                ss = ss & "-" & o1
            End If       
        Next
        Log(ss)
        ss = ""
    Next

The last row is not displayed in the Log

HELP!

:)
 

Daestrum

Expert
Licensed User
Longtime User
I don't think it could be the reason , but who knows?

The only difference I can see is where we initialise the lists

you use Li.initialize()

and I use l.initialize << no parenthesis
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You could try clearing the selection after you load the data into the tableview, then try selecting.
Maybe when adding the data, it automatically highlights the last entry added.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

find attached an example tested using selected rows. the last row is selected and shown in the log. Please give a try if this works.
 

Attachments

  • TableViewSelectionMode.zip
    2.8 KB · Views: 253
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Something is not right when selecting rows.
You can run your example and it will select rows plus last one as you coded (which is what the OP didn't want, his was selected without being selected).
You can run it again with no changes and it can fail to select any rows. Run it again without changes and the selection works again.
Curious.....
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Got the point = Can confirm indeed strange behavior. Ran my example few times by calling the jar and in cases no rows were selected at all and other cases selection was as defined. Have no idea about the cause.
 
Upvote 0
Top