浓咖啡。执行 'load adapter data' 时出错

Espresso. Error performing 'load adapter data'

我有一个 ListView,它显示来自数据库的数据。

    db = new DB(this);
    db.open();


    String[] from = new String[]{DB.COLUMN_FIRSTNAME, DB.COLUMN_LASTNAME};
    int[] to = new int[]{android.R.id.text1, android.R.id.text2};        

    scAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_activated_2, null, from, to, 0);
    lvData = (ListView) findViewById(R.id.lvData);
    lvData.setAdapter(scAdapter);

    lvData.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    lvData.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

它将数据库中的名字和姓氏显示为项目列表: Click to UI

所以,今天我尝试在这个应用程序中使用 Espresso,但我找不到点击包含文本的项目的方法。

当我使用:

onData(anything())
    .inAdapterView(withId(R.id.lvData))
    .atPosition(3)
    .perform(click());

它工作得很好。但是我想点击包含相应项目文本的项目。

到目前为止我尝试了什么(我在 Whosebug、google、github 等处找到的所有内容):

onView(allOf(withText("Ivan Ivanov"))).perform(click())

onData(allOf(is(instanceOf(MainActivity.class)),is("Ivan Ivanov")))
            .inAdapterView(withId(R.id.lvData))
            .perform(click());

onData(hasToString(startsWith("v")))
            .inAdapterView(withId(R.id.lvData))
            .atPosition(0).perform(click());

onData(instanceOf(MainActivity.class))
            .inAdapterView(withId(R.id.lvData))
            .atPosition(0)
            .check(matches(hasDescendant(withText("Ivan Ivanov"))));

onData(anything()).inAdapterView(withContentDescription("Ivan Ivanov"))
            .atPosition(0).perform(click());

所以,字符串 "Ivan Ivanov" 和包含数据库数据的项目之间可能存在差异:firstName+lastName?

使用 CursorMatchers 匹配列表视图中的项目。

onData() 适用于适配器中的 data,而不是实际视图。在您的情况下,ListView 由 CursorAdapter 支持,因此是 CursorMatcher。

用法如下:

onData(withRowString(DB.COLUMN_FIRSTNAME, "Ivan")).perform(click());