如何在ListView - Robotium自动化中点击一个索引在10位置的按钮?

时间:2012-10-19 06:40:28

标签: java android robotium

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

4 个答案:

答案 0 :(得分:1)

尝试这样做(不确定它是否有效)

//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

希望这有帮助!

答案 1 :(得分:0)

答案 2 :(得分:0)

我不确定你到底想要做什么,我的假设是你有一个列表视图,有太多的项目可以放在屏幕上,你想要点击位于第10个位置的按钮或者其他东西影响?我是对的吗?

如果是这样,我之前已经生成了一些listview帮助函数来获取列表视图中给定索引的视图:

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

答案 3 :(得分: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);