user1667968
user1667968

Reputation: 1515

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

Suppose, I have a ListView, which contains 20 ListItems. Each item is having a button, now I want to click a button which is located at 10 position in ListView. How I can automate it via robotium?

Upvotes: 4

Views: 2173

Answers (4)

Dan Riza
Dan Riza

Reputation: 477

Try to do ti like this (not sure if it works)

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

Hope this helps !

Upvotes: 1

Naman
Naman

Reputation: 1

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

Upvotes: 0

Paul Harris
Paul Harris

Reputation: 5819

I am not sure exactly what you are trying to do, My assumption is that you have a list view with too many items to fit on the screen and you want to click the button that is at the 10th position or something to that effect? am i right?

If so i have previously produced some listview helper functions to get the view at a given index in the list view:

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

Upvotes: 0

Proghero
Proghero

Reputation: 669

Try using the solo.clickInList(int line, int index)

Something like:

solo.clickInList(10,0)

Hope this helps!

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)

Upvotes: 0

Related Questions