0

I have the following codes to traverse a ListView in AndroidViewClient to build a list of accounts. It works fine but is it a good way to do because I can't find a more proper way to pass the variable list_accounts to the function findAccount() as it raises an Argument error so I must use it globally. Is there a way to pass a parameter to the transform method of vc.traverse() ?

def findAccount(view):
    if view.getClass() == 'android.widget.TextView':
        text = view.getText()
        if re.match(re.compile('.*@yahoo.com'), text):
            list_accounts.append(text)

list_accounts = []
listview_id = vc.findViewByIdOrRaise('id/no_id/11')
vc.traverse(root=listview_id, transform=findAccount)
for item in list_accounts:
    print "account:", item
AbrtFus
  • 29
  • 7

1 Answers1

1

You can do this

def findAccount(la, view):
    if view.getClass() == 'android.widget.TextView':
        text = view.getText()
        if re.match(re.compile('.*@yahoo.com'), text):
            la.append(text)

list_accounts = []
listview_id = vc.findViewByIdOrRaise('android:id/list')
vc.traverse(root=listview_id, transform=lambda v: findAccount(list_accounts, v))
for item in list_accounts:
    print "account:", item

but I'm not sure this is more clear and readable than your version.

However, you can do

for tv in vc.findViewsWithAttribute('class', 'android.widget.TextView', root=listview_id):
    text = tv.getText()
    if re.match(re.compile('.*@yahoo.com'), text):
        list_accounts.append(text)

which I guess improves readability.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • that's perfect as usual from you, thanks. I will implement your last solution, it is more natural and readable. On a side note I usually use the traverse() method with a transform custom function but for specific actions like touch(), it works very well, but in that case of updating a variable it is not appropriate. – AbrtFus May 15 '15 at 08:25