B4J Question TableView Add from TextField

ThRuST

Well-Known Member
Licensed User
Longtime User
I want to add text that is typed into a textfield into the first column of a tableview, however the tableview only displays the same name for each item I add. Sometime tries ends up with an empty string.
Can single items be added or must all items be handled in a for each loop and added together with the new string (object?) So, how to properly add items to a tableview?

It seems like DButils might be the better way, to store data in SQLite then list the contents with DBUtils. Am I right?
 
Last edited:

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
I do not fully understand the question.

But each row in the tableview is a mere a array, so if you want to change text of the first column it should be something like:

B4X:
for each r() as string in tv.items
 r(0) = textfield.text
next

tv.setcolumnvisible(0,false)
tv.setcolumnvisible(0,true)
'This instructions will force the tableview to refresh the info
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I want to add text that is typed into a textfield into the first column of a tableview...

I tried your solution but that did not work, I assume your code should be

B4X:
for each r() as string in tv.items
r(0) = textfield.text
next

tv.items.add(r)

tv.setcolumnvisible(0,false)
tv.setcolumnvisible(0,true)
'This instructions will force the tableview to refresh the info

That returned an ignored string.
Since TableView uses an array of strings, tv.items.add(r) might not be correct. Please correct your code.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I have covered them all but still I have not found a way how to dynamically insert/delete text from a textfield into a new row in tableview without using DButils...
The easiest way is to manipulate the database contents by SQL quieries then have everything listed with DButils.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Little example that may help you.
B4X:
Sub Process_Globals
 Private fx As JFX
 Private MainForm As Form
 Dim ta As TableView
 Dim tf As TextField
 Dim li As List
End Sub
Sub AppStart (Form1 As Form, Args() As String)
 MainForm = Form1
 MainForm.Show
' just create a tableview and textfield
 tf.Initialize("tf")
 ta.Initialize("")
 ta.SetColumns(Array("one","two","three"))
 ta.SetColumnWidth(0,100)
 ta.SetColumnWidth(1,100)
 ta.SetColumnWidth(2,100)
 MainForm.RootPane.AddNode(tf,10,10,100,20)
 MainForm.RootPane.AddNode(ta,10,40,300,400)
 
li.Initialize
' list li will contain the contents of the tableview - they will remain linked
' if you add an item to the list it will appear in the tableview
 li = ta.Items
End Sub

'add the textfield content to the list, and hence the tableview
Sub tf_Action
 li.Add(Array(tf.Text," "," "))
 tf.Text = ""
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
B4X:
'add the textfield content to the list, and hence the tableview
Sub tf_Action
li.Add(Array(tf.Text," "," "))
tf.Text = ""
End Sub

Daestrum, I haven't tried your example yet but from analyze it's easy to understand. This got to be one of the best examples I have ever seen in this forum so far.
If I could just visually know how the data is stored into the list it would be complete. I think the LIST links the data into columns. For now this is superb.
Hat off for this one, thanks :)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Daestrum, have you tested your code? somehow I didn't get it to work when I implemented it in my project, so I guess I must have made a misstake somewhere.
I will have to start a new project just to try your example from scratch. I guess I was confused by thinking of Erels tutorial with Row (columns) as Object.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The Tableview in my example contains three columns, therefore the data item in the list must also have three values per item.
I used the line
B4X:
li.Add(Array(tf.Text," "," "))

This effectively creates an Object that has the characteristics of the tableview row.
A string with the value from the textfield, a string ' ' and another string ' '

If you wanted four columns for your tableview you could use
B4X:
li.Add(Array(tf.Text," "," "," "))
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Daestrum, I got it to work thanks to your latest post. But it only works without the textfield. If I use it, the cell remains empty.
I have added the textfield visually in Scene builder and loads the form, so maybe there's an issue with that. I assume your example works right out of the box.
TableView seems similar to inserting into a database, it is vital that the number of items is added, or it will generate an error. Extra error messages would be great.
Really great example, appreciate it alot thanks.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
btw my project uses four columns, so that in itself caused some trouble. Thanks for pointing this out as well.
I have posted a question about DButils which seems to be the obvious next step, and it would be great to see your replies about that :)
You can find it here
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Daestrum, I created a new project just to try your solution and it generated this error. I pasted all your code into a new project. Did it work for you?

Note: I did not use the internal designer or the scene builder.


B4X:
Sub tf_Action
Try
li.Add(Array(tf.Text," "," "))
tf.Text = ""
   
Catch
    Log(LastException)
End Try
End Sub

(RuntimeException) java.lang.RuntimeException: Object should first be initialized (List).
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
table.PNG


Daestrum, Sorry I forgot to add the last past of your code. This is what it looks like.
It works right out of the box, nice. How do you catch the selected index? ta.selectedindex ?
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
TextField does not seem to work with scene builder in the above example. If causes the first column to be empty. Not even when I add textfield as a node to existing panes. If someone can make it work with Scenebuilder let me know.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I retried my example using the built in designer and it works exactly the same way.

How do you catch the selected index? ta.selectedindex ?

I think it's
B4X:
Sub TableViewReference_SelectedRowChanged(Index As Int, Row() as Object)

if Index = -1 then no row has been selected.
 
Upvote 0
Top