【问题标题】:Get text of TextView inside RecyclerView - android instrumental test在 RecyclerView 中获取 TextView 的文本 - android 仪器测试
【发布时间】:2016-08-10 12:42:54
【问题描述】:

我有一个包含 item.xml 项目的 RecyclerView。 在 item.xml 我有一个 TextView。

我正在创建一个仪器测试,并想知道如何从 recyclerView 的那个 textview 中获取文本,以便我可以用它做一些额外的工作。

使用https://spin.atomicobject.com/2016/04/15/espresso-testing-recyclerviews/,我设法检查了我期望的文本是否在我期望的位置,但我也需要获取值。

错误代码:

descendant: with string from resource id: <2131427419>' doesn't match the selected view when calling onView(withRecyclerView(R.id.recycler_view).atPosition(2)) .check(matches(hasDescendant(withText(R.id.my_textview))));

【问题讨论】:

  • 您注意到了吗:onView(withRecyclerView(R.id.scroll_view).atPosition(3)) .check(matches(hasDescendant(withText("Some content"))));?要获取TextView 的值,请使用ViewMatchers.withText() 方法
  • 不过,withText(resourceId) 将返回一个 ViewInteraction,不明白我如何从中获取字符串。 (新测试)同样,我在调用onView(withRecyclerView(R.id.recycler_view).atPosition(2)) .check(matches(hasDescendant(withText(R.id.my_textview))));时收到错误descendant: with string from resource id: &lt;2131427419&gt;' doesn't match the selected view
  • 请编辑您的帖子并将此代码添加为 EDIT
  • 我也需要这个。 4 年后仍然没有答案。

标签: android testing android-espresso


【解决方案1】:

试试这个

String title = ((TextView) recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.my_textview)).getText().toString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多