I\'m trying to run some automated tests in Robotium by iterating through a list and clicking on each list element to start another activity. I have the code below in my test
I have previously used these helper functions in a slightly different state to handle most of what you need with listviews:
public View getViewAtIndex(final ListView listElement, final int indexInList, Instrumentation instrumentation) {
ListView parent = listElement;
if (parent != null) {
if (indexInList <= parent.getAdapter().getCount()) {
scrollListTo(parent, indexInList, instrumentation);
int indexToUse = indexInList - parent.getFirstVisiblePosition();
return parent.getChildAt(indexToUse);
}
}
return null;
}
public void scrollListTo(final T listView,
final int index, Instrumentation instrumentation) {
instrumentation.runOnMainSync(new Runnable() {
@Override
public void run() {
listView.setSelection(index);
}
});
instrumentation.waitForIdleSync();
}
With these functions stored somewhere (they can be static if you like... i prefer to not do that but it is convenient)
ListView list = solo.getCurrentListViews().get(0);
for(int i=0; i < list.getAdapter().getCount(); i++){
solo.clickOnView(getViewAtIndex(list, i, getInstrumentation()))
solo.assertCurrentActivity("Json Class", JsonActivity.class);
solo.goBack();
}
Your current solution is in fact trying to iterate through all the listviews you have on screen and not the elements in the listView.