2012-10-19 26 views
4

假设,我有一个ListView,其中包含20个ListItems。每个项目都有一个按钮,现在我想单击位于ListView中10个位置的按钮。我如何通过robotium实现自动化?如何点击一个按钮,该按钮在ListView - Robotium自动化中的10位置索引?

+0

家庭作业?听起来应该很容易。 –

+0

所以,有趣的告诉我解决方案 – user1667968

+0

如果我知道robotium,我会。这就是为什么我保守并说“听起来像”。 –

回答

0

尝试使用solo.clickInList(INT线,诠释指数)

喜欢的东西:

solo.clickInList(10,0) 

希望这有助于!

http://www.jarvana.com/jarvana/view/com/jayway/android/robotium/robotium-solo/2.0.1/robotium-solo-2.0.1-javadoc.jar!/com/jayway/android/robotium/solo/Solo.html#clickInList(int,%20int)

+0

非常感谢...我不想点击10位置的列表,我想点击一个位于列表10位置的按钮。你有区别吗? – user1667968

+0

是的,我明白了。我现在无法测试它,但我认为它会起作用。你是否试图通过ID搜索按钮,好像它不在列表中或类似的东西? – Proghero

0

我不知道正是你正在尝试做的,我的假设是,你有太多的项目的列表视图,以适应在屏幕上,你要点击的按钮是在第十位还是有这个效果的?我对吗?

如果是我以前产生了一些列表视图辅助函数在列表视图中给定索引处得到的观点:

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(); 
} 
1

尝试做TI这样的(不知道它的工作原理)

//get the list view 
ListView myList = (ListView)solo.getView(R.id.list); 
//get the list element at the position you want 
View listElement = myList.getChildAt(10);// myList is local var 
//click on imageView inside that list element 
solo.clickOnView(solo.getView(listElement.findViewById(R.id.my_button)));// not double eE 

希望这有助于!

+0

在Robotium 4.3.1中,这是无法完成的,因为solo.clickOnView()采用'int'类型参数,但solo.getView()返回'View'。 –

0
//First get the List View 
    ListView list = (ListView) solo.getView(R.id.list_view); 

/*  View viewElement = list.getChildAt(10); 
     This might return null as this item view will not be created if the 10th element is 
     not in the screen. (i.e. the getView would have not been called for this view). 

     Suppose for single item list_item.xml is used then 
     Get the 10th button item view as follows:*/ 
    int i = 10 ;  

    View buttonItem = list.getAdapter().getView(i,getActivity().findViewById(R.layout.list_item),list); 

    solo.clickOnView(buttonItem); 
相关问题