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?
Asked
Active
Viewed 2,170 times
4
-
Homework? Sounds like it should be easy. – John Dvorak Oct 19 '12 at 06:43
-
So, funny tell me the solution – user1667968 Oct 19 '12 at 06:46
-
I would if I knew robotium. That's why I'm conservative and say "sounds like". – John Dvorak Oct 19 '12 at 06:47
-
so, robotium is a Java library? If so, tag `[java]`. – John Dvorak Oct 19 '12 at 06:53
-
assuming latest version. I'm downloading the documentation. – John Dvorak Oct 19 '12 at 06:54
-
The first part is easy, accessing ListView by index: http://developer.android.com/reference/android/view/ViewGroup.html#getChildAt(int) – John Dvorak Oct 19 '12 at 07:15
-
See, I didn't ask, how we can access ListView by index in android, I am asking abt robotium automation to access the ListView by index. – user1667968 Oct 19 '12 at 07:27
4 Answers
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 !
-
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!

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