B4J Question How to customize the table view

IslandMedic

Member
Licensed User
Longtime User
I want to use a table view for showing incoming message from the server. It seems like a good tool to use, has alot of formatting controls etc all built in. Here is my code and here are questions.

B4X:
'setup the table view for messaging
OpTableView1.SetColumns(Array As String("Time","Sender","Message"))
OpTableView1.SetColumnSortable(0,True) 'can sort by time
OpTableView1.SetColumnSortable(1,True) 'can sort by sender
OpTableView1.SetColumnSortable(2,False) 'cannot sort by message
OpTableView1.SetColumnWidth(0,60) 'fixes the time column width.
OpTableView1.SetColumnWidth(0,100) 'fixes the time column width.

Public Sub WriteMsg(msg AsString,from AsString)Dim row(3) As Object
  row(0) = DateTime.Time(DateTime.Now)
  row(1) = from
  row(2) = msg
  'add the new items OpTableView1.Items.Add(row)'sort the time column to put the newest message at the top
End Sub

1. I want the new message to be at the top of the view. I know I can sort the column by clicking on the header, but can I do it in code? So what I would do is insert the row and then get it to sort by time for the user so the newest message is always at the top.

2. I only want to have three columns, so doing this OpTableView1.SetColumnVisible(3) etc doesn't seem practical, how would I just have three?

3. the third column it would be nice to have the size of it be the remaining percentage of the table view window.

thanks in advance,

Brad

IslandMedic, Yesterday at 2:32 PM
 

DonManfred

Expert
Licensed User
Longtime User
I have had a similar problem here.
The solution was to use a custom Type and a list of these Types to store the message.

B4X:
    Type smsMessages (Time As Long,from As String,Message As String)

Now create a Global list to hold all SMS in form of a List of Types
B4X:
Dim smslist As List
smslist.Initialize

For each message you get you create a instance of your Type and put it to the global list.

B4X:
Public Sub NewMessage(message As String,from As String, date As Long)
    Dim msg As smsmsg
    msg.Initialize
    msg.Message = message
    msg.from = from
    msg.Time = date
    smslist.Add(msg)
End Sub

Now you can sort your List with
B4X:
smslist.SortType("date",True) ' or false

Last but not least you refill the table with the content of your list

B4X:
    table.Items.Clear
    For i = 0 To smslist.Size -1
        Dim msg As smsmsg = smslist.Get(i)
        table.Items.Add(Array As Object(msg.Time, msg.from, msg.Message))
    Next
 
Upvote 0
Top