Android Question How to refer to controls inside a panel which is inside the customlistview?

Hanz

Active Member
We can put a panel inside the listview(xcustomlistview) as cell item. The panel may contain controls, eg. button, label, etc. We use this panel and the controls therein many times. My question is, how can we refer to them individually?

In case, the question is confusing, let me illustrate.

B4X:
+----------------------------+
|   label1   label2          |   <---------- a panel with two labels
+----------------------------+

+----------------------------+
|   the panel as list item   |    label2.text = "dog"
+----------------------------+
| the panel is used again    |  label2.text = "cat"
+----------------------------+
| the panel used 3rd time    |   label2.text = "mouse"
+----------------------------+

Now, I will go to label1_click and say, I do like this:
B4X:
log(label2.text)

If I click label1 of the three items, I always get the value of label2 of the third listitem and that is "mouse". How can I refer to individual labels and get their specific/individual values or properties
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
B4X:
dim lbl as B4xview = xclv.GetView(1)
Log(lbl.text)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User

If you follow this tutorial, the first video (jump to 15:50) you will see, you can use
B4X:
Dim index As Int = CLV1.GetItemFromView(Sender)
to get the list item you click on.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I think that the problem is that the panel is re-used rather than a new panel for each item. Check that you are using a new clone of the panel.

Probably in your case something like:

B4X:
        Dim pnl As B4XView = xui.CreatePanel("")
        pnl.LoadLayout("CLVCell2")
 
Upvote 0

Hanz

Active Member
I think that the problem is that the panel is re-used rather than a new panel for each item. Check that you are using a new clone of the panel.

Probably in your case something like:

B4X:
        Dim pnl As B4XView = xui.CreatePanel("")
        pnl.LoadLayout("CLVCell2")
This is exactly what I did.
 
Upvote 0

Hanz

Active Member

If you follow this tutorial, the first video (jump to 15:50) you will see, you can use
B4X:
Dim index As Int = CLV1.GetItemFromView(Sender)
to get the list item you click on.
Thanks, I get the index, my problem now is to get to a specific control, in this case the label.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
My take on the OP's question ...

B4X:
Sub Label1_Click
    Dim Index As Int = clv1.GetItemFromView(Sender)
    Dim pnl As B4XView = clv1.GetPanel(Index)   'the base row item panel ...

    'you first have to access your designer panel ... then the child views. (labels)
    Log(pnl.GetView(0).GetView(0).Text)    '1st label on designer panel.. refer order in Designer ViewTree for Item/Row layout
    Log(pnl.GetView(0).GetView(1).Text)    '2nd label

A little extra posted code would have clarified if you are using Label1_Click or CLV1_Click event.
If you are using the latter then remove the 1st line of code as the index is returned automatically.
 
Upvote 0

Hanz

Active Member
My take on the OP's question ...

B4X:
Sub Label1_Click
    Dim Index As Int = clv1.GetItemFromView(Sender)
    Dim pnl As B4XView = clv1.GetPanel(Index)   'the base row item panel ...

    'you first have to access your designer panel ... then the child views. (labels)
    Log(pnl.GetView(0).GetView(0).Text)    '1st label on designer panel.. refer order in Designer ViewTree for Item/Row layout
    Log(pnl.GetView(0).GetView(1).Text)    '2nd label

A little extra posted code would have clarified if you are using Label1_Click or CLV1_Click event.
If you are using the latter then remove the 1st line of code as the index is returned automatically.
For your code at line 6, I get this error
B4X:
java.lang.RuntimeException: Object should first be initialized (View).
I also use the Label1_Click, and I'm getting right the first lines of codes except the part similar to your code at line 6 where the above error message appears.
 
Upvote 0

Hanz

Active Member
My take on the OP's question ...

B4X:
Sub Label1_Click
    Dim Index As Int = clv1.GetItemFromView(Sender)
    Dim pnl As B4XView = clv1.GetPanel(Index)   'the base row item panel ...

    'you first have to access your designer panel ... then the child views. (labels)
    Log(pnl.GetView(0).GetView(0).Text)    '1st label on designer panel.. refer order in Designer ViewTree for Item/Row layout
    Log(pnl.GetView(0).GetView(1).Text)    '2nd label

A little extra posted code would have clarified if you are using Label1_Click or CLV1_Click event.
If you are using the latter then remove the 1st line of code as the index is returned automatically.

Here is the screenshot of the output:

1625442268867.png
 
Upvote 0

Hanz

Active Member
Found the answer...
B4X:
Dim index As Int = lvw.GetItemFromView(Sender)
Dim pnl As B4XView = lvw.GetPanel(index)
Dim lbl As B4XView = pnl.GetView(2) <----- the index is 2 because that is the order of the label in the designer in my case.
Log(lbl.Text)
Thanks for your replies.
 
Upvote 0
Top