我有一个 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/