B4J Question [Solved] Listview Pane Class Public Variable

AHilton

Active Member
Licensed User
Longtime User
I have a form that has a listview in which I add a class multiple times. The class returns a pane that is added to the listview like ...

form code ...

B4X:
for x = 1 to 10
  private address as clsAddress
  address.initialize
  address.IamHere = True          ' a Public Variable in the clsAddress class
  lvAddresses.Items.Add(address.asPane)        ' The Listview
next

class code ...

B4X:
Sub Class_Globals
  Private ThisPane as Pane
  Public IamHere as Boolean
End Sub
Public Sub asPane as Pane
  ThisPane.Initialize("ThisPane")
  ThisPane.LoadLayout("Address")
  Return ThisPane
End Sub

How do you read later, from the form, the value of IamHere since all I am storing in the listview is the PANE of that class?
 

Cableguy

Expert
Licensed User
Longtime User
The problem in your code is that you are adding the same instance of your class, initializing it 10x over...
Of course, it works as you intented, but you get no way to reference it afterwards...
Try this:

B4X:
Dim address(10) as clsAddress
for x = 0 to 9
  address(x).initialize
  address(x).IamHere = True          ' a Public Variable in the clsAddress class
  lvAddresses.Items.Add(address(x).asPane)        ' The Listview
next
Then you can get your variable read using
myVar=address(1).IamHere
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
Sorry. That was just pseudo-code. And, I am creating new instances of my class with the "private address as clsAddress" part, yes? The problem, as I'm seeing it now, is that I have nothing to hold a reference to the entire object within the form. I have a reference to the PANE being returned from that class (within the listview), though, which isn't letting me see the variables contained in the class itself.

Everything works (many subs, events, variables and even other classes referenced within that class) within the class itself, and the several instances of that class in the listview, but it's when I'm trying to get to anything in that class other than the pane and views being loaded into it by the loadlayout() is where I'm having a problem. Such as, from the form, trying to iterate over each Address object (clsAddress class) in the listview to see if a public variable has changed. It's not just initializing one instance of the class several times over.

I think your suggestion gave me the hint I was looking for, though. I have to keep the entire class instance somewhere in order to reference it later (ie dim address(10) as clsAddress) instead of just relying on the PANE of the class getting me there.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Mine was also pseudo code, but if you declare your class variables as public, you can access them as long as you have a ref to the entire class.
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
Yep. That fixed it.

I was relying on that returned PANE from the class stored in the listview to allow me to get to the public variables and subs of the class because I was able to get to them while IN the class (a textfield could call any of the subs or reference any of the variables in its' own class). Bad assumption.

Gotta hold a reference to the whole class in order to get to constructs.

Thanks, Cableguy.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
don't forget to change the title and add "[Solved]"
 
Upvote 0
Top