B4J Question [BANano] Iteration with For Each failed

Kiffi

Well-Known Member
Licensed User
Longtime User
It's me again

(using BANano 1.02)

I have a class ListView:
B4X:
Sub Class_Globals
    Public Items As List
End Sub

Public Sub Initialize
    Items.Initialize
End Sub

Public Sub AddItem(Item As ListViewItem)
    Items.Add(Item)
End Sub

I have a class ListViewItem:
B4X:
Sub Class_Globals
    Public Text As String
End Sub

Public Sub Initialize
    Text = ""
End Sub

and i have this code to add items to the ListView:
B4X:
Dim myListView As ListView
myListView.Initialize

For Counter = 0 To 9
    Dim myListViewItem As ListViewItem
    myListViewItem.Initialize
    myListViewItem.Text = "ListViewItem " & Counter
    myListView.AddItem(myListViewItem)
Next

now i want to iterate over all items in myListView:
B4X:
For Each myLVI As ListViewItem In myListView.Items
    Log(myLVI.Text)
Next

However the transpiled JavaScript doesn't iterate over the items but over the ListView:
B4X:
// [73]  For Each myLVI As ListViewItem In myListView.Items
for (_mylvi in _mylistview) {
// [74]  Log(myLVI.Text)
console.log(_mylvi._text);
// [75]  Next
}

Thanks in advance & Greetings ... Peter
 

alwaysbusy

Expert
Licensed User
Longtime User
I don't think this works yet (a sub variable as the 'IN'). Try this (untested, I'm not at my PC):

B4X:
Dim tmpList as List = myListView.Items
For Each myLVI As ListViewItem In tmpList
    Log(myLVI.Text)
Next

For now, sometimes you will have to break the code a bit down to simpler objects I'm afraid. B4X wasn't build in a day either
The more BANano evolves, the closer I will get it to all B4J can do. This is a prime example of what I will look for in the next version of BANano.
 
Upvote 0

Kiffi

Well-Known Member
Licensed User
Longtime User
Try this [...]
Then I get an array of ListViewItems. When I iterate over this, myLVI only contains the array indexes.

It's too complicated for me. Instead I will iterate classically with a counter until further notice.

B4X:
For Counter = 0 To myListView.Items.Size - 1
    Dim myLVI As ListViewItem
    myLVI = myListView.Items.Get(Counter)
    Log(myLVI.Text)
Next

For now, sometimes you will have to break the code a bit down to simpler objects I'm afraid.
no problem. Right now it's hard for me to see if I might have done something wrong, or if it's a feature yet to be implemented, or if it's a bug.

But don't hesitate to report such cases!

ok, i'll be back!

Thanks & Greetings ... Peter
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…