B4J Question Retrieving the "Name" property of a custom view

nbarakat

Member
Hello community, and thanks for a great product @Anywhere Software.

I am creating a custom view in B4J that requires the Name property set by the user in the designer grid. I know I can get the event name, text properties through the lbl, size properties through Base, as well as the custom properties through Prop.

Is there any way of getting the Name property for the control whether set through the designer or initiated in a dim statement (Dim x as myControl:x.initialize)?

I also need a unique ID for each instance of the created control. Any suggestions what I can use for this?

My custom control keeps track of each control added in a map. I was using the EventName to distinguish between the different controls but as could be expected this doesn't work well when one shares the same Event or when they use the same control name in different container (i.e. Control1 in Pane1 and control1 in Pane2 both loaded under Tab1 and Tab2).
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

nbarakat

Member
Thank you @Erel for your insight.

I have tried using DesignerUtils, but I am unable to retrieve the name of the custom control and its container without using the designer. Additionally, it seems like the user needs to use DesignerUtils to trigger the code, which might be inconvenient.

My custom view is created programmatically and not through the designer.

I am trying to create a custom view in B4J that allows me to get the control name and parent container programmatically, so I can ensure uniqueness and allow the user to link between controls easily just by providing the control's name. (I also do keep track of an internal ID for each created view that does provide uniqueness but not helpful to the user).

I am wondering if there is a Java call or reflection method that I can use to retrieve the control name and parent from Base or Callback. I am aware that I can get the memory location, which is unique, but it is not helpful for allowing the user to define the relationship between controls.

Any suggestions on how to achieve this would be greatly appreciated. Thank you!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Objects do not have names.
2. The designer views names are not unique. Layouts can be loaded multiple times. DesignerUtils solves it based on the parent scope (with a few reasonable assumptions).
3. Creating layouts programmatically is a big mistake (in most cases) and isn't cross platform. This is especially true in B4J and B4i where you need to handle the page being resized.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
3. Creating layouts programmatically is a big mistake (in most cases) and isn't cross platform. This is especially true in B4J and B4i where you need to handle the page being resized.
and if you then decide to insert a new view and you have to "move" everything by hand...!
Some members prefer to create a layout programmatically, but then waste days ? tweaking the various Left, Top, Width, Height
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
When you create the custom view, you can save a reference to the view. You can then put this in a map where the key is the name you would want to use.

You can create the view either programmatically or via a layout (recommended) using this sort of technique

You can then "reference" any custom view using the "name" via the key/value from the map.
 
Upvote 0

nbarakat

Member
and if you then decide to insert a new view and you have to "move" everything by hand...!
Some members prefer to create a layout programmatically, but then waste days ? tweaking the various Left, Top, Width, Height
I am not a fan of creating layouts programmatically. But my custom view is done programmatically as it basically has a text field and a list view and a couple of encapsulating panes. I will attach the view in a sample to demonstrate the issue better and see if someone could suggest a solution (I do need a couple of days to cleanup code! ... The view I am doing in B4X I did over 25 years ago and still works in VB6 and currently I am doing its only for B4J as a learning curve and maybe more. I just can't believe how hard it seems to do what seems like a simple thing ... but probably its a lack of knowledge on my side!
 
Upvote 0

nbarakat

Member
When you create the custom view, you can save a reference to the view. You can then put this in a map where the key is the name you would want to use.

You can create the view either programmatically or via a layout (recommended) using this sort of technique

You can then "reference" any custom view using the "name" via the key/value from the map.
Yes I do that and a little more.

B4X:
Public Sub SaveControl(Uniq As Int,cParent As Object, cBase As vsListBox, cDesignerName As String)
    If ControlLst.IsInitialized=False Then
        ControlLst.Initialize
    End If
    ControlLst.Add(Array As Object(Uniq, cDesignerName.ToLowerCase, cParent, cBase))    
End Sub

'Using EventName as cDesignerName...Not Correct
Public Sub GetLinkedControl(cTargetControlName As String, cParent As Control) As vsListBox
    For I=0 To ControlLst.Size-1
        Dim Rec() As Object=ControlLst.Get(I)
        Dim cntNum As Int=Rec(0).As(Int)
        Dim cDName As String=Rec(1).As(String)
        ' The passed
        If cDName<>cTargetControlName.ToLowerCase Then Continue
        Dim cPar As Object=Rec(2)
        If cPar <> cParent Then Continue 'Same Target Name but not same Parent so its not it
        'If Same Parent and Same Name return the control
        Dim Cntrl As vsListBox=Rec(3).As(vsListBox)
       
        Return Cntrl      
    Next
    Return Null
End Sub

I even have a Unique ID (Basically a counter on the number of views added).

Basically what I am trying to do is to allow the user to be able to set a relationship between instances of the custom view in a generic way (when needed).

For example one view would hold a list of regions, another list of countries, and a third list of cities. So basically when the region selection changes, countries and cities change transparent to the developer and user. So basically to do this countries has to be aware of changes in regions, and cities should be aware in changes in countries ... I think it will be more apparent when I put a simple example out.

Another way to do it which is an over kill I think might be like MQTT broker when a view changes it broadcast its change and each control registers to receive notifications from different entities.
 
Upvote 0

nbarakat

Member
Ok So here it is. A small demo of the custom view. So the view works fine when the user doesn't change the default event name in the designer. This event name is used by the user to set relationships between different custom views in the layout. I know one solution might be to add a Name property to the custom properties of the view but that wouldn't be convenient to the user to keep track of multiple ways to identify the view.

It would be great if the Name parameter either when set through the designer or initiated through a Dim vsc as CustomView, The Name or "vsc" are passed within the prop map.

The view loads layouts set and defined by the user to allow more complex ways of saving data to a DB table.

It still needs some work and was hastily adapted to work with sqlite instead of mySQL so it can be easier to test on the forum.

This is a first attempt on a custom view and it is designed only for B4J. Need to research more on adapting it to B4A and maybe B4i (which I am unfamiliar with). I know for one thing I shouldn't be using the Listview.

b4xrss.db is the sample sqlite sample database. I tested using sqlite-jdbc-3.7.2.jar.
 

Attachments

  • vsListBoxFragments.zip
    182.5 KB · Views: 112
Upvote 0

nbarakat

Member
Sorry Community,

The code in post #8 had a couple of bugs when loading a user defined layout (The code probably has more bugs and definitely could use optimization).
Attached is a corrected version.
Thanks
 

Attachments

  • vsListBoxFragments.zip
    182.9 KB · Views: 106
Upvote 0
Top