iOS Question Dynamically generating textview with unique name

Ross Woodward

Member
Licensed User
Longtime User
Hi there,

Hope you can help.

I have code that generates a list of products with qtybox[textview] & plus/minus buttons.
Now, when I click on plus button it adds value to qtybox for the very last item.

I believe as the name for the textview is qt and it's repeated so it's confused.

So, I believe if have textview with unique name it will let me add the value to the appropriate box. Any ides how to go around programmatically create textview with unique name?

Unless you have any other suggestions.
Thanks

Jiri
 

stevel05

Expert
Licensed User
Longtime User
You need to post the code that creates the textviews, it sounds like you are not creating multiple views, but reusing the same object. You need to Dim each one before adding it.

It will be easier to help if you post your code. And don't forget to use code tags so it's formatted on the forum.
[ code] code here [ /code] without the spaces within the brackets.
 
Upvote 0

Ross Woodward

Member
Licensed User
Longtime User
I cannot dim all the textview [boxes] as I the list is populated from database, so it can be more than 30 at one point.

Hence, i want to create the textview in run time, depending on the RowCount from the database.

If you still need to code, I will post it.. but I think my best bet is to create textview depending on the rowcount.
Thanks
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
However you do it, you will need to Dim and initialize each textview, If you are creating an array the Dim will be

B4X:
Dim TV(RowCount) As TextView

Then in the loop where you are presumably adding the content you will need to initialize each element.

B4X:
For i = 0 to RowCount - 1
    TV(i).Initialize("TV")
    TV(i).Text = ?
    TV.Tag = "TV" & i
... etc
Next

If you need more help then please post the code.
 
Upvote 0

Ross Woodward

Member
Licensed User
Longtime User
However you do it, you will need to Dim and initialize each textview, If you are creating an array the Dim will be

B4X:
Dim TV(RowCount) As TextView

Then in the loop where you are presumably adding the content you will need to initialize each element.

B4X:
For i = 0 to RowCount - 1
    TV(i).Initialize("TV")
    TV(i).Text = ?
    TV.Tag = "TV" & i
... etc
Next

If you need more help then please post the code.

Here's part of the code - that's I think is relevant.

B4X:
SQResultSet = SQL.ExecQuery("SELECT id,itemname,code FROM productlist WHERE categoryid = '" & CategoryID & "' ")
   
   
    Do While SQResultSet.NextRow

            sItemName = SQResultSet.GetString("itemname")
            sItemCode = SQResultSet.GetString("code")
     
            pnl_ItemList  'this creates all the panel and labels to store the names,codes.

       
    Loop
    SQResultSet.Close

B4X:
    Add.Initialize("Add") 'Image view add'
    Add.Width = 48
    Add.Height = 48
    Add.Bitmap = LoadBitmap(File.DirAssets,"plus.png")

    Minus.Initialize("Minus")
    Minus.Width = 48
    Minus.Height = 48
    Minus.Bitmap = LoadBitmap(File.DirAssets,"minus.png")
   
   
    ItemQtyPanel.AddView(Add, 120, 1, 46, 46)
    ItemQtyPanel.AddView(Minus, 4, 1, 46, 46)
   
    Qty.Initialize("") 'that would be the Qty Textview
    Qty.Width = 46
    Qty.Height= 46
    Qty.Text = iQty
   
    ItemQtyPanel.AddView(Qty, 62, 1, 46, 46)
.

B4X:
Sub Add_Click ' add Qty of 6
           
        iQty = iQty + 6   

    UpdateUI
End Sub

Sub UpdateUI  'Refresh TextView to get value.
   
        Qty.Text = iQty

   
End Sub

I think that's the relevant part.

So in the getting the records from DB, I will have to run for each inside the While Loop, to create each TextView for .NextRow.

Then the last bit, adding value to textview, I need to make sure the add buttons add it's to appropriate textview.

Thanks for the help!
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Where are you Dimming Add, Minus and Qty?
 
Upvote 0

Ross Woodward

Member
Licensed User
Longtime User
Where are you Dimming Add, Minus and Qty?

At the top, sorry.. I forgot about those.
I have managed to get the TextView to be created with Unique tag. [Thanks!]

I'm trying to get the each imageview/button to add or remove value to appropriate TextView box.
When ever I add it add's it to the last box.

The code for "add" is above, I then call the update after I click.
I have also made sure they get appropriate unite tag name.


B4X:
RowID = 0
    ScrollView1.Panel.RemoveAllViews
    x = 0
   
    SQResultSet = SQL.ExecQuery("SELECT id,itemname,code FROM productlist WHERE categoryid = '" & CategoryID & "' ")
   
   
   
    Do While SQResultSet.NextRow
   
            sItemName = SQResultSet.GetString("itemname")
            sItemCode = SQResultSet.GetString("code")
            testid = testid + 1
            pnl_ItemList
           
           
            TV.Initialize("TV")
            TV.Tag = RowID
            TV.Text = iQty & TV.tag
            TV.Width = 46
            TV.Height = 46
            ItemQtyPanel.AddView(TV, 62, 1, 46, 46)
           
           
            Add.Initialize("Add", Add.STYLE_SYSTEM)
            Add.Tag = "Add" & RowID
            Add.Width = 48
            Add.Height = 48
            Add.Text = RowID
            'Add.Bitmap = LoadBitmap(File.DirAssets,"plus.png")

            Minus.Initialize("Minus" , Minus.STYLE_SYSTEM)
            Minus.Tag = "Minus" & RowID
            Minus.Width = 48
            Minus.Height = 48
            Minus.Text = RowID
            'Minus.Bitmap = LoadBitmap(File.DirAssets,"minus.png")
   
            ItemQtyPanel.AddView(Add, 120, 1, 46, 46)
            ItemQtyPanel.AddView(Minus, 4, 1, 46, 46)
           
            RowID = RowID + 1
       
    Loop
    SQResultSet.Close

I then have the variable declared on the top section.

Thank you!
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
As I said, you need to Dim a new Object each time, try this:
B4X:
RowID = 0
    ScrollView1.Panel.RemoveAllViews
    x = 0
 
    SQResultSet = SQL.ExecQuery("SELECT id,itemname,code FROM productlist WHERE categoryid = '" & CategoryID & "' ")
 
 
 
    Do While SQResultSet.NextRow
 
            sItemName = SQResultSet.GetString("itemname")
            sItemCode = SQResultSet.GetString("code")
            testid = testid + 1
            pnl_ItemList
         
            Dim TV AS TextView
            TV.Initialize("TV")
            TV.Tag = RowID
            TV.Text = iQty & TV.tag
            TV.Width = 46
            TV.Height = 46
            ItemQtyPanel.AddView(TV, 62, 1, 46, 46)
         
            Dim Add As Button
            Add.Initialize("Add", Add.STYLE_SYSTEM)
            Add.Tag = "Add" & RowID
            Add.Width = 48
            Add.Height = 48
            Add.Text = RowID
            'Add.Bitmap = LoadBitmap(File.DirAssets,"plus.png")

            Dim Minus As button
            Minus.Initialize("Minus" , Minus.STYLE_SYSTEM)
            Minus.Tag = "Minus" & RowID
            Minus.Width = 48
            Minus.Height = 48
            Minus.Text = RowID
            'Minus.Bitmap = LoadBitmap(File.DirAssets,"minus.png")
 
            ItemQtyPanel.AddView(Add, 120, 1, 46, 46)
            ItemQtyPanel.AddView(Minus, 4, 1, 46, 46)
         
            RowID = RowID + 1
     
    Loop
    SQResultSet.Close
 
Upvote 0

Ross Woodward

Member
Licensed User
Longtime User
Okay, in this case I won't be able to call the TV.text to update it, when I click on add.

What's the work around for that? Thanks again.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
as @stevel05 already said you need to declare every time a new textview

if you want to update the textview you can do it like this

(because you set the same Tag to the button and to the textview on the same process you can do it like this)

B4X:
Sub Add_Click
    Dim b As Button = Sender

    Dim tv_tag As String = b.Tag
    tv_tag = tv_tag.Replace("Add","")

    For Each tv As textview In ItemQtyPanel.GetAllViewsRecursive
        If tv.tag = tv_tag Then
            'do here what ever you want
            Exit
        End If
    Next
End Sub
 
Last edited:
Upvote 0

Ross Woodward

Member
Licensed User
Longtime User
as @stevel05 already said you need to declare every time a new textview

if you want to update the textview you can do it like this

(because you set the same Tag to the button and to the textview on the same process you can do it like this)

B4X:
Sub Add_Click
    Dim b As Button = Sender

    Dim tv_tag As String = b.Tag.Replace("Add","")
  
    For Each tv As textview In ItemQtyPanel.GetAllViewsRecursive
        If tv.tag = tv_tag Then
            'do here what ever you want
            Exit
        End If
    Next
End Sub


I have error regarding a missing library?

Dim tv_tag As String = b.Tag.Replace("Add","") [missing library?]

Do I need to activate special library?
 
Upvote 0

Ross Woodward

Member
Licensed User
Longtime User
Sorry my mistake

Try this

B4X:
Sub Add_Click
    Dim b As Button = Sender
    Dim tv_tag As String = b.Tag
tv_tag = tv_tag.Replace("Add","")

    For Each tv As textview In ItemQtyPanel.GetAllViewsRecursive
        If tv.tag = tv_tag Then
            'do here what ever you want
            Exit
        End If
    Next
End Sub


Thanks that solves one error, but I now I have error in the log, when I click on Add sign.


====== Combo1 Item selected 'Size A Landscape' =========
Error occurred on line: 475 (Main)
Expected: UITextView, object type: UIButton
Stack Trace: (
CoreFoundation <redacted> + 150
libobjc.A.dylib objc_exception_throw + 38
CoreFoundation <redacted> + 0
RFG-[B4IObjectWrapper setObject:] + 364
CoreFoundation <redacted> + 68
CoreFoundation <redacted> + 292
RFG+[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1786
RFG-[B4IShell runVoidMethod] + 210
RFG-[B4IShell raiseEventImpl:method:args::] + 2154
RFG-[B4IShellBI raiseEvent:event:params:] + 1340
RFG__33-[B4I raiseUIEvent:event:params:]_block_invoke + 74
libdispatch.dylib <redacted> + 10
libdispatch.dylib <redacted> + 22
libdispatch.dylib <redacted> + 1524
CoreFoundation <redacted> + 8
CoreFoundation <redacted> + 1574
CoreFoundation CFRunLoopRunSpecific + 520
CoreFoundation CFRunLoopRunInMode + 108
GraphicsServices GSEventRunModal + 160
UIKit UIApplicationMain + 144
RF Gallerymain + 108
libdyld.dylib <redacted> + 2
)

Thanks.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Try like this


B4X:
Sub Add_Click
    Dim b As Button = Sender
    Dim tv_tag As String = b.Tag
    tv_tag = tv_tag.Replace("Add","")

    For Each v As view In ItemQtyPanel.GetAllViewsRecursive
      If v is textview then
         If v.tag = tv_tag Then
            'do here what ever you want
            Exit
         End if
      End If
    Next
End Sub
 
Last edited:
Upvote 0

Ross Woodward

Member
Licensed User
Longtime User
Try like this


B4X:
Sub Add_Click
    Dim b As Button = Sender
    Dim tv_tag As String = b.Tag
     tv_tag = tv_tag.Replace("Add","")

    For Each v As view In ItemQtyPanel.GetAllViewsRecursive
      If v is textview then
      If tv.tag = tv_tag Then
            'do here what ever you want
            Exit
       End if
  
        End If
    Next
End Sub


Thanks, I have colleague to help and we have tried your code, and then edited it a bit, but still no joy.

I'm attaching a file.
To explain it more. What I'm looking for is, for each item we have in list, the textview will have tocorrespond to each button [+/-] that I can add/remove value.
So when i press add on line 2, it will add 6 to textview on line 2.

At the moment, it keeps doing the last line aka ID3.

If you want more info let me know.

Thanks
 

Attachments

  • IMG_0016.PNG
    IMG_0016.PNG
    16.9 KB · Views: 187
Upvote 0

stevel05

Expert
Licensed User
Longtime User
As you are adding the views to a panel before adding them to the main view, you also need to Dim a new panel for each iteration. The attached file works.
 

Attachments

  • rw.zip
    2.2 KB · Views: 164
Upvote 0

ilan

Expert
Licensed User
Longtime User
can you upload your project (only with the part we are dealing)

EDIT: (i took part on this discussion with my phone thats why i didnot get the whole picture,
and its hard to say what you are doing wrong in your code. a small project will help us to help you)
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
ok now when i look at your code there are some basic mistakes

1, if you add a view to a panel or page you set the view height and width so you dont need to do that twice

B4X:
TV.Width = 46
TV.Height = 46
ItemQtyPanel.AddView(TV, 62, 1, 46, 46)

2, in your code you add those views in a loop and i dont know how many rows you have but you add all views always to the same position. why?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
here is a working example (i tried it on the simulator):

B4X:
'Code module
#Region  Project Attributes
    #ApplicationLabel: B4i Example
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Dim addtvbtn As Button
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
  
    addtvbtn.Initialize("addbtn",addtvbtn.STYLE_SYSTEM)
    addtvbtn.Text = "click me"
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    Page1.RootPanel.AddView(addtvbtn,0,0,100,50)
End Sub

Private Sub Application_Background
  
End Sub

Sub addbtn_click

    For i = 1 To 5
        Dim TV As TextView
        TV.Initialize("TV")
        TV.Tag = i
        TV.Text = "iQty" & TV.tag
   
        Dim Add As Button
        Add.Initialize("Add", Add.STYLE_SYSTEM)
        Add.Tag = "Add" & i
        Add.Text = "Add" & i
        'Add.Bitmap = LoadBitmap(File.DirAssets,"plus.png")

        Dim Minus As Button
        Minus.Initialize("Minus" , Minus.STYLE_SYSTEM)
        Minus.Tag = "Minus" & i
        Minus.Text ="Minus" &  i
        'Minus.Bitmap = LoadBitmap(File.DirAssets,"minus.png")

        Page1.RootPanel.AddView(TV, 62, i*50, 46, 46)
        Page1.RootPanel.AddView(Add, 120, i*50, 46, 46)
        Page1.RootPanel.AddView(Minus, 4, i*50, 46, 46)              
    Next
  
End Sub

Sub Add_click
    action("Add")
End Sub

Sub Minus_click
    action("Minus")
End Sub

Sub action(txt As String)
    Dim b As Button = Sender
    Dim b_tag = b.Tag, tv_tag = "" As String
    b_tag = b_tag.Replace(txt,"")

    For Each v As View In Page1.RootPanel.GetAllViewsRecursive
      If v Is TextView Then
         Dim tv As TextView = v
         tv_tag = tv.Tag
         If tv_tag = b_tag Then
            tv.Text = txt & " clicked"
         End If
      End If
    Next
End Sub
 
Last edited:
Upvote 0
Top