Android Question To GetView or not to GetView and how to get View index values?

Mashiane

Expert
Licensed User
Longtime User
Hi there...

I am loading the same layout with with different values to the customlistview and or scrollview. For example, for each "map" data record I have, I load the layout to a panel then I am tagging the views like this for every item I am adding. There are multiple data records between 1-10. Everything seems to work well.

B4X:
' set the view tags
    Dim pan As Panel
    pan.Initialize("")
    Activity.AddView(pan,0dip,0dip,100%x,100%y)
    pan.LoadLayout("followingeach")
    pan.Tag = jMap
    user_to = jMap.Get("id")
    user_to_name = jMap.Get("username")
    jMap.Put("user_to", user_to)
    jMap.Put("user_to_name", user_to_name)
    jMap.Put("user_from", auser_id)
    jMap.Put("user_from_name", auser_name)
    lblType.Text = "Search..."
    imgUserLogo.Tag = "user" & user_to
    lblUserName.Tag = "lblUserName_" & user_to
    lblUserName.Text = user_to_name
    btnToday.Tag = "btnToday_" & user_to
    btnFollowUser.Tag = "follow_" & user_to
    btnTrayUser.Tag = "tray_" & user_to
    btnNewsFeeds.Tag = "newsfeeds"
    btnDay.Tag = "day"
    btnProfile.Tag = "profile"
    btnTray.Tag = "tray"
    btnTrayO.Tag = "trayo"
    imgLoad.Tag = "load_" & user_to
    imgLoad.Visible = True

The example image of my view is attached herein.

I have seen in some examples that one can follow the GetView approach like this..

B4X:
dim lbl as label = pan.GetView(0)
lbl.tag = "xxx"

using the index of the view to tag it etc. The thing is with layouts with multiple views like mine, how does one know which view has which index then to pass that to GetView?

The reason I'm asking, for some reason, which the approach I have used, the tag values are lost and I'm trying to find the cause. I will test with moving my variable declarations to Process_Globals for now.

My questions are:

  1. With GetView, how does one determine the index of each view added to the layout?
  2. Is tagging by view name e.g. btnTrayUser.Tag = "tray_" & user_to, for such cases where the same layout is loaded with different values a better approach than GetView?
 

Attachments

  • tag.png
    tag.png
    14.9 KB · Views: 484

Erel

B4X founder
Staff member
Licensed User
Longtime User
Panel.GetView(0) will return the first child view. If you are adding the items with the designer then it is the first view in the views tree.

However you don't need it. Add global variables with the names of the views.
Whenever you load a layout file those variables will point to the last views added (based on the names).
You can use these variables to set the tags or to add the views to a different collection.
 
Upvote 0

vbmundo

Well-Known Member
Licensed User
Hi Erel,

I have the same problem with the CustomListView

B4X:
ub OnOff_CheckedChange(Checked As Boolean)
    Dim index As Int= ClvAlarmas.GetItemFromView(Sender)
    Dim pnl As Panel = ClvAlarmas.GetPanel(index)
    Dim chk As ACSwitch = pnl.GetView(2)
    Msgbox($"Item value: ${ClvAlarmas.GetValue(index)}
Check value: ${chk.Checked}"$, "")

In

pnl.GetView(2)

Are any way to reference by View Name ? some like that

pnl.GetView("MyView")

Regards
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Are any way to reference by View Name ? some like
No
You can use the TAG to find a view

Start with creating NEW THREAD. This thread is issued by @Mashiane and you should have created a NEW thread for your issue....

Basic Rule: Always(!) create a new Thread for any new question
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
How ? I don't see how
Use the forumsearch. this is handled hundred times....

similar code:
B4X:
For Each v As View In Activity.GetAllViewsRecursive
        If v Is EditText Then
            Dim et As EditText = v
            et.Text = ""
        End If
Next
 
Upvote 0

vbmundo

Well-Known Member
Licensed User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
DonManfred, but this code don't return the INDEX !!
I KNOW!!!!!!

It shows you the way yo need to go to get all views (you need to add the check for the tag by yourself!)

You´ll not get the index. You´ll find THE VIEW. You can - if yoou want - store it´s index in the tag to get it later.
 
Last edited:
Upvote 0

vbmundo

Well-Known Member
Licensed User
Then B4A haven't the way to Know what INDEX have every VIEW with a simply line code ?

Like

Label1.index (VB6 and VB.NET)

I need to set this value in TAG on every Designer modification...

The problem is that my Layout are already created.

Regards
 
Upvote 0
Top