4

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?

maszter
  • 3,680
  • 6
  • 37
  • 53
user1667968
  • 1,489
  • 3
  • 12
  • 16

4 Answers4

1

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 !

Jemshit
  • 9,501
  • 5
  • 69
  • 106
Dan Riza
  • 397
  • 3
  • 11
  • In Robotium 4.3.1, this cannot be done, because solo.clickOnView() take `int` type parameter but solo.getView() returns `View`. – Nari Kim Shin Dec 10 '13 at 23:48
0

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)

Proghero
  • 659
  • 1
  • 9
  • 20
  • Thanks for that... I don't want to click a list at 10 position, i want to click a button, which is located in list at 10 position. do u got the difference? – user1667968 Oct 19 '12 at 12:12
  • Yes, I see. I'm not able to test it right now, but I thought it would work. Have you tried to search for the button by ID as if it wasn't inside a list or something like that? – Proghero Oct 19 '12 at 12:57
0

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();
}
Paul Harris
  • 5,769
  • 1
  • 25
  • 41
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);
Naman
  • 1