Android Question Programaticaly select Row on Table

Roger Daley

Well-Known Member
Licensed User
Hi All,

I have SQL db/list/webview I am trying to have the Webview/table "Select" a the Row of a newly added record. The following pictures from a modified SQL2 example may explain this better:

Screen1.jpg
Screen1 shows 4 records the second line selected.

Screen2.jpg
Screen2 shows a newly added record. By default the first line is selected. I want the latest record to be selected.
IE

SCreen3.jpg

I have worked out a way to do this but it is messy and convoluted. Before I code a complete mess my question is:

Is there a function/option to go to the new record as if the WebView has been clicked?
This is a bit odd as it is arse about to the normal operation. I think I am searching for something that does not exist, but worth asking.

Thanks in advance
 

sorex

Expert
Licensed User
ain't it just

B4X:
mytable.SelectedRow=mytable.Items.Size 'maybe -1
?
 

sorex

Expert
Licensed User
wait... you wrote webview? why do you have the table in a webview?
 

klaus

Expert
Licensed User
In Sub ReadDataBaseRowIDs replace this code
B4X:
If RowIDList.Size > 0 Then
    CurrentIndex = 0                            'set the current index to 0
Else
    CurrentIndex = -1                            'set the current index to -1, no selected item
    ToastMessageShow("No items found", False)
End If
ResultSet1.Close                                                'close the ResultSet, we don't need it anymore
by this one.
B4X:
If RowIDList.Size > 0 Then
    If CurrentIndex = -1 Or CurrentIndex > RowIDList.Size - 1 Then
        CurrentIndex = 0                            'set the current index to 0
    End If
Else
    CurrentIndex = -1                            'set the current index to -1, no selected item
    ToastMessageShow("No items found", False)
End If
ResultSet1.Close                                                'close the ResultSet, we don't need it anymore
 

Roger Daley

Well-Known Member
Licensed User
In Sub ReadDataBaseRowIDs replace this code
B4X:
If RowIDList.Size > 0 Then
    CurrentIndex = 0                            'set the current index to 0
Else
    CurrentIndex = -1                            'set the current index to -1, no selected item
    ToastMessageShow("No items found", False)
End If
ResultSet1.Close                                                'close the ResultSet, we don't need it anymore
by this one.
B4X:
If RowIDList.Size > 0 Then
    If CurrentIndex = -1 Or CurrentIndex > RowIDList.Size - 1 Then
        CurrentIndex = 0                            'set the current index to 0
    End If
Else
    CurrentIndex = -1                            'set the current index to -1, no selected item
    ToastMessageShow("No items found", False)
End If
ResultSet1.Close                                                'close the ResultSet, we don't need it anymore

Thanks Klaus,
I'm away from the computer at the moment so will be a while before I get back.
Just looking at it is encouraging.

Regards Roger
 

Roger Daley

Well-Known Member
Licensed User
In Sub ReadDataBaseRowIDs replace this code
B4X:
If RowIDList.Size > 0 Then
    CurrentIndex = 0                            'set the current index to 0
Else
    CurrentIndex = -1                            'set the current index to -1, no selected item
    ToastMessageShow("No items found", False)
End If
ResultSet1.Close                                                'close the ResultSet, we don't need it anymore
by this one.
B4X:
If RowIDList.Size > 0 Then
    If CurrentIndex = -1 Or CurrentIndex > RowIDList.Size - 1 Then
        CurrentIndex = 0                            'set the current index to 0
    End If
Else
    CurrentIndex = -1                            'set the current index to -1, no selected item
    ToastMessageShow("No items found", False)
End If
ResultSet1.Close                                                'close the ResultSet, we don't need it anymore

Klaus,

Unfortunately not quite right. The two attached screen shots Before and After of adding record 3.

Before.jpg After.jpg

I guess what this shows is that I need a way to set the currentindex to match the highest RowID.
That I have added an "Order By" to the SUB makes it a bit more complex.

B4X:
'Reads the database rowids in RowIDList
Private Sub ReadDataBaseRowIDs
    Private ResultSet1 As ResultSet
    
    If Filter.flagFilterActive = False Then
        ResultSet1 = SQL1.ExecQuery("SELECT rowid FROM persons ORDER BY UPPER(LastName) ASC")
    Else
        ResultSet1 = SQL1.ExecQuery("SELECT rowid FROM persons"  & Filter.Query)
    End If
    
    'We read only the rowid column and put them in the IDList
    RowIDList.Initialize                                'initialize the ID list
    Do While ResultSet1.NextRow
        RowIDList.Add(ResultSet1.GetInt2(0))        'add the rowid's to the RowID list
    Loop
    If RowIDList.Size > 0 Then
        'CurrentIndex = 0                            'set the current index to 0
        If CurrentIndex = -1 Or CurrentIndex > RowIDList.Size - 1 Then
            CurrentIndex = 0                            'set the current index to 0
        End If

    Else
        CurrentIndex = -1                            'set the current index to -1, no selected item
        ToastMessageShow("No items found", False)
    End If
    ResultSet1.Close                                                'close the ResultSet, we don't need it anymore
End Sub
Regards Roger
 

klaus

Expert
Licensed User
I tested it with my SQLiteLight2 program and it worked.
You hadn't said in your first post that your list is sorted.

Try the attached project. It works with the rowIDs.
 

Attachments

Roger Daley

Well-Known Member
Licensed User
Brilliant Klaus,

Sorry about missing to tell you it was sorted, in my mind I had but the only clue was the screen shots. A bit vague.

All working well and looks simpler than what I was trying to do.

Regards Roger.
 
Top