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

Suppose I have a ListView that contains 20 ListItems. Each item has a button, now I want to click the button, which is located at position 10 in the ListView. How can I automate it using robotics?

+4
source share
4 answers

Try to do so (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!

+1
source

I'm not sure exactly what you are trying to do. I assume that you have a list with too many elements to fit on the screen, and you want to press a button located at 10th position, or something in this effect? I'm right?

If so, I previously created some helper functions for listview to get a view at a given index in a 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(); } 
0
source
  //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. (ie 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); 
0
source

Source: https://habr.com/ru/post/1440710/


All Articles