浓咖啡.执行'加载适配器数据'时出错

pli*_*osv 14 android android-espresso

我有一个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() {
Run Code Online (Sandbox Code Playgroud)

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

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

我用的时候:

onData(anything())
    .inAdapterView(withId(R.id.lvData))
    .atPosition(3)
    .perform(click());
Run Code Online (Sandbox Code Playgroud)

它完美地运作.但我想点击包含相应项目文本的项目.

到目前为止我尝试了什么(我在stackoverflow,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());
Run Code Online (Sandbox Code Playgroud)

因此,字符串"Ivan Ivanov"和项目之间可能存在差异,该项目包含来自数据库的数据:firstName+ lastName

Be_*_*ive 7

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

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

用法如下:

onData(withRowString(DB.COLUMN_FIRSTNAME, "Ivan")).perform(click());
Run Code Online (Sandbox Code Playgroud)