简体   繁体   English

Android Espresso ListView 点击项

[英]Android Espresso ListView click item

I have ListView with pictures and text.我有带有图片和文字的ListView 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 ?我可以使用Adapter位置单击ListView ,而不使用匹配或startWith吗?

Try with atPosition().尝试使用 atPosition()。 eg例如

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.索引为 0 时,它将单击找到的第一个匹配视图。

Use Record Test to obtain the ViewInteraction of the list then obtain the view and use performItemClick as follows:使用 Record Test 获取列表的 ViewInteraction 然后获取视图并使用 performItemClick 如下:

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));
    });
}

试试这个:

onView(withText("ListItemText")).perform(ViewActions.click());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM