android uiautomator to click ListView

RagHaven picture RagHaven · Jun 7, 2013 · Viewed 9.5k times · Source

I have an android app which uses the uiautomator to click the options in a listview. The ListView looks like this:

enter image description here

I am trying to click the Full Benchmark list item, but my code for it does not recognize the list item. This is what I have:

UiScrollable listView = new UiScrollable(new UiSelector().scrollable(
        true).className("android.widget.ListView"));

UiObject item1 = listView.getChildByText(new UiSelector()
.className(android.widget.TextView.class.getName()),
        "Full Benchmark");

item1.click();

I would appreciate any help!

Answer

Frank Charlton picture Frank Charlton · Jun 14, 2013

Here is what I use to find, and then click an item in a listview:

//Find and click a ListView item
public void clickListViewItem(String name) throws UiObjectNotFoundException {
    UiScrollable listView = new UiScrollable(new UiSelector());
    listView.setMaxSearchSwipes(100);
    listView.scrollTextIntoView(name);
    listView.waitForExists(5000);
    UiObject listViewItem = listView.getChildByText(new UiSelector()
            .className(android.widget.TextView.class.getName()), ""+name+"");
    listViewItem.click();
    System.out.println("\""+name+"\" ListView item was clicked.");
}

So in your case it would be

clickListViewItem("Full Benchmark")

Or:

UiScrollable listView = new UiScrollable(new UiSelector());
listView.setMaxSearchSwipes(100);
listView.scrollTextIntoView(name);
listView.waitForExists(5000);
UiObject listViewItem = listView.getChildByText(new UiSelector()
        .className(android.widget.TextView.class.getName()), "Full Benchmark");
listViewItem.click();