Click all the list view elements while scrolling using robotium

后端 未结 3 690
情深已故
情深已故 2020-12-17 06:58

I have a listView that contains lots of elements i.e. we have to scroll down to see all the elements. Now what i want to do is, click all the listView elements. How can I do

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-17 07:52

    It looks like your code, as currently implemented, is only considering the visibile list items when controlling the loop and handling the clicking. It's important to note the behavior of two things:

    First, there's a concept called view recycling in Android that helps conserve memory when dealing with ListViews. Only the views that are currently on screen are created, and once they scroll off the screen they'll be repopulated with new data. Therefore, calling methods like getChildCount and getChildAt on a ListView will only perform these operations on the visible items. To find information about the data that populates the list, you can call methods such as getCount() or getItem() on the ListView's adapter.

    Second, the clickInList() method is 1-indexed, relative to the current position of the list, and can only be used for visible items. As far as I know, it will never scroll your list automatically. This means that calling clickInList(2) when at the top of the list will click the second item, but then calling clickInList(2) again when the 30th item is at the top of the list will click the 32nd.

    Knowing these two things, your solution will need to consider all of the list data and perhaps have a bit more precision when making clicks. Here's how I'd rewrite your while loop to ensure you'll be able to handle every item on the list, hope this helps:

    ListAdapter adapter = l.getAdapter();
    for(int i=0; i < adapter.getCount(); i++) 
    {
        //Scroll down the list to make sure the current item is visible
        solo.scrollListToLine(l, i);
    
        //Here you need to figure out which view to click on.
        //Perhaps using adapter.getItem() to get the data for the current list item, so you know the text it is displaying.
    
        //Here you need to click the item!
        //Even though you're in a list view, you can use methods such as clickOnText(), which might be easier based on how your adapter is set up
    
        solo.goBack();
    }
    

提交回复
热议问题