for each loop error occured when called inside a function

Viewed 22

I wanted to iterate through checkboxes and tap on the checkboxes. I wrote foreach loop without using a function and its working but when I had given foreach loop inside a method its showing error as foreach not applicable to type 'io.appium.java_client.android.AndroidElement' .

Code:

For Each loop without using a method - Working

List<AndroidElement> checkbox = (List<AndroidElement>) driver.findElementsById("io.appium.android.apis:id/tasklist_finished");

for (AndroidElement check : checkbox) {               
            tap(check);     
        }

For Each loop inside a method - Not Working

List<AndroidElement> checkbox = (List<AndroidElement>) driver.findElementsById("io.appium.android.apis:id/tasklist_finished");

Checkboxes((AndroidElement) checkbox);      

public static void Checkboxes(AndroidElement checkbox){                             
        for (AndroidElement check : checkbox) {       **-->Issue**   
            tap(check);      
        }

Error: java: for-each not applicable to expression type required: array or java.lang.Iterable found: io.appium.java_client.android.AndroidElement

foreach not applicable to type 'io.appium.java_client.android.AndroidElement'

1 Answers

Do not iterate over AndroidElement type, iterate over list:

List<AndroidElement> checkbox = (List<AndroidElement>) driver.findElementsById("io.appium.android.apis:id/tasklist_finished");    
Checkboxes(checkbox); 
public static void Checkboxes(List<AndroidElement> checkbox)
{                             
        foreach (AndroidElement check in checkbox)
        {          
            tap(check);      
        } 
}
Related