Android Espresso ListView click item

[亡魂溺海] 提交于 2019-12-09 17:03:02

问题


I have ListView with pictures and text. When I try to click item, I get error

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.cifrasoft.telefm:id/cardsGridView' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.

I use the following code:

onData(hasToString(startsWith("Item Text")))
            .inAdapterView(withId(R.id.cardsGridView))
            .perform(click());

Can I click ListView using position of Adapter, without matches or startWith?


回答1:


Try with atPosition(). e.g.

onData(hasToString(startsWith("Item Text")))
            .inAdapterView(withId(R.id.cardsGridView)).atPosition(0)
            .perform(click());

with index 0, it will click on the first matching view found.




回答2:


Use Record Test to obtain the ViewInteraction of the list then obtain the view and use performItemClick as follows:

AtomicReference<ListView> resultView = new AtomicReference<>(null);
ViewInteraction viewInteraction1 = onView( ... withId(R.id.my_list_id), ...);
viewInteraction1.check(((view, noViewFoundException) -> {
    if(noViewFoundException != null){
        return;
    }

    resultView.set((ListView) view);
}));

if(resultView.get() != null){
    ListView listView = resultView.get();
    activity.runOnUiThread(()->{
        listView.performItemClick(
            listView.getAdapter().getView(index, null,null),
            index,
            listView.getAdapter().getItemId(index));
    });
}


来源:https://stackoverflow.com/questions/28019843/android-espresso-listview-click-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!