android - Espresso 。执行错误 'load adapter data'

标签 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() {

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

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

当我使用时:

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

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

到目前为止我尝试了什么(我在 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());

那么,字符串“Ivan Ivanov”和包含数据库数据的项目之间是否存在差异:firstName+lastName

最佳答案

使用CursorMatchers匹配 ListView 中的项目。

onData() 作用于适配器中的数据,而不是实际 View 。在您的例子中,ListView 由 CursorAdapter 支持,因此是 CursorMatcher。

用法如下:

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

关于android - Espresso 。执行错误 'load adapter data',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37030336/

相关文章:

java - 向 Android 应用程序添加通话按钮

android - 有什么方法可以从应用程序自动发送邮件吗?

android - 应用程序运行时不显示 Activity 标题

android - Android 浏览器中的溢出隐藏、边界半径和变换

android - 为什么 Espresso 在测试完成后离开应用程序?如何阻止它这样做

android - 确定 Espresso 测试时的 fragment 变化

android - 膨胀类 com.google.android.material.floatingactionbutton.FloatingActionButton 仪器测试时出错

java - 以编程方式创建的 TextView 未显示

android - 运行 Android Espresso 测试错误,您需要在此 Activity 中使用 Theme.AppCompat 主题(或后代)

android - 使用 android espresso 测试具有多个 fragment 的 ViewPager