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
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.