浓咖啡.执行“加载适配器数据"时出错 [英] Espresso. Error performing 'load adapter data'
问题描述
我有一个ListView,它显示数据库中的数据.
I have a ListView, which shows data from a database.
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() {
它以项目列表的形式显示数据库中的名字和姓氏: 点击到用户界面
It shows the first name and last name from the database as a list of items: Click to UI
因此,今天,我尝试在此应用程序中使用Espresso,但找不到找到包含文本的项目的方法.
So, today I tried to use Espresso with this app and I can't find a way to click on the item containing the text.
当我使用时:
onData(anything())
.inAdapterView(withId(R.id.lvData))
.atPosition(3)
.perform(click());
它完美地工作.但是我想单击包含相应项目文本的项目.
It works perfectly. But I want to click on the item containing the corresponding item's text.
到目前为止,我一直在尝试(我在stackoverflow,google,github等网站上找到的所有内容):
What I tried so far (everything I've found at stackoverflow, google, github, etc.):
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
?
So, maybe is there are differences between the string "Ivan Ivanov" and the item, which contains data from the database: firstName
+lastName
?
推荐答案
使用光标匹配器以匹配列表视图中的项目.
Use CursorMatchers to match items in your listview.
onData()
适用于适配器中的数据,而不适用于实际视图.在您的情况下,ListView由CursorAdapter(因此为CursorMatcher)支持.
onData()
works against the data in your adapter, not the actual view. In your case the ListView is backed by the CursorAdapter, hence the CursorMatcher.
用法如下:
onData(withRowString(DB.COLUMN_FIRSTNAME, "Ivan")).perform(click());
这篇关于浓咖啡.执行“加载适配器数据"时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!