簡體   English   中英

如何在ListView - Robotium自動化中點擊一個索引在10位置的按鈕?

[英]How to click on a button, which is indexed at 10 position in ListView - Robotium automation?

假設,我有一個ListView,它包含20個ListItems。 每個項目都有一個按鈕,現在我想點擊一個位於ListView中10位置的按鈕。 我如何通過robotium自動化它?

嘗試這樣做(不確定它是否有效)

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

希望這可以幫助 !

我不確定你到底想要做什么,我的假設是你有一個列表視圖,有太多的項目可以放在屏幕上,你想點擊第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();
}
    //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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM