Iterating through a List and clicking on list items in Robotium

让人想犯罪 __ 提交于 2019-11-29 12:47:01

I see a couple of problems:

  1. The code is iterating over all list views in the activity, but it is not doing anything with the current list view.
  2. As the code does not show the value of chartPosition, there is no guarantee, the current list has that many items.
  3. I believe you need to wait for the new activity to load.

Try this:

  for(int i = 0; i < list.size(); i++) {
      assertTrue("There are no listviews in this activity.", list.size() > 0);
      chartPosition = 0;  // just to be safe, point at the first item in the list.
      for(int i = 0; i < list.size(); i++) {
         solo.clickInList(chartPosition, i);  // Note that "i" identifies the ListView

         solo.waitForActivity("name.of.the.expected.activity");

      }
     ...
  }

Disclaimer - This suggestion is based purely on code inspections and Robotium Solo Javadoc: http://www.jarvana.com/jarvana/view/com/jayway/android/robotium/robotium-solo/1.4.0/robotium-solo-1.4.0-javadoc.jar!/com/jayway/android/robotium/solo/Solo.html#clickInList(int)

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 <T extends AbsListView> 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!