B4J Question Tableview edit

davelew1s

Active Member
Licensed User
Longtime User
Hi!
I have created a tableview and populated it from a file, how do i edit individual cells after loading?
Thanks Dave.
 

rwblinn

Well-Known Member
Licensed User
Longtime User
B4X:
Dim row() As Object = tv.Items.Get(<row number>)
row(3) = "new value"
Hi Erel,

noticed that the tableview is not refreshed automatically after changing data.

Question: It would require a routine like I made below OR is there a tv.Refresh routinr?

B4X:
' To refresh the tableview (id: tableviewStandard) displayed data with any new data
Sub tableviewStandard_Refresh
    ' Get the selected row
    Dim rs As Int = tableviewStandard.SelectedRow
    ' Use a temporary list which holds the updated row (cells) objects as taken from the updated tableview list
    Dim tvl As List
    tvl.Initialize
    tvl.AddAll(tableviewStandard.Items)
    ' Clear all items in the tableview list
    tableviewStandard.Items.Clear
    ' And add the objects from the list to the tableviewStandard items
    For i = 0 To tvl.Size - 1
        Dim r() As Object = tvl.Get(i)
        tableviewStandard.Items.Add(Array As Object(r(0), r(1)))
      Next
    ' Set the selected row
    tableviewStandard.SelectedRow = rs
End Sub
 
Upvote 0

davelew1s

Active Member
Licensed User
Longtime User
Hi!
Thanks for both replies I managed Erel solution but could not get it to change the table...now i know because it does not refresh. I cannot see any refresh, maybe Erel will help.
Thanks Dave.
 
Upvote 0

davelew1s

Active Member
Licensed User
Longtime User
Hi!
Erel your solution works OK but my table has about 12 columns and all may need editing, yours only used 1 column, I could not work out how to put labels in all 'cells'.
rwblinn, your solution is the easiest but the table does not refresh, the item list contains the new data but no refresh.
I am still continuing with both options. Thanks again Dave.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It should be simple to make all the values Labels.

This sub will add a row of strings to the table as a row of labels:
B4X:
Sub AddRow(tv As TableView, Row() As String)
   Dim rowOfLabels(Row.Length) As Object
   For i = 0 To Row.Length - 1
     Dim lbl As Label
     lbl.Initialize
     lbl.Text = Row(i)
     rowOfLabels(i) = lbl
   Next
   tv.Items.Add(rowOfLabels)
End Sub
Now all you need to do when editing an item, is to get the Row of objects, convert the correct one to Label and change its text.
 
Upvote 0

davelew1s

Active Member
Licensed User
Longtime User
Hi Erel!
Thanks for the sample code it worked ok however it caused problems further down the code, but it has great potential for other projects.
So I used your first suggestion and managed to force the tableview to refresh I used this:-

Sub tableRefresh
Table1 .SetColumnVisible (0,False) 'Forces a table refresh
Table1 .SetColumnVisible (0,True)
End Sub
it might not be the correct way but it works and is only 2 lines.
Thanks for all your help on this problem but'I'll be back'
Dave.
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Sub tableRefresh
Table1 .SetColumnVisible (0,False) 'Forces a table refresh
Table1 .SetColumnVisible (0,True)
End Sub
This method doesn't work if you use TextField in the rows of the TableView. :-(

[EDIT]
The only method I found to refresh the tableView with Textfield is using a timer.

B4X:
Sub Process_Globals
    Private tv1 As TableView
    Private TimerRefresh As Timer
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    TimerRefresh.Initialize("TimerRefresh", 1)
End Sub

Sub tv1StartRefresh
    For i = 0 To tv1.ColumnsCount -1
        tv1.SetColumnVisible(i, False)
    Next
    TimerRefresh.Enabled = True
End Sub

Sub TimerRefresh_Tick
    For i = 0 To tv1.ColumnsCount -1
        tv1.SetColumnVisible(i, True)
    Next  
    TimerRefresh.Enabled = False
End Sub

Because hiding/showing the column in the same sub doesn't work. B4J is missing a "DoEvent" like in B4A. The problem here is that there is an ugly 1ms moment where the columns are hidden....
 
Last edited:
Upvote 0
Top