Espresso. Error performing 'load adapter data'

ぐ巨炮叔叔 提交于 2019-12-22 01:56:16

问题


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

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.

When I use:

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.

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());

So, maybe is there are differences between the string "Ivan Ivanov" and the item, which contains data from the database: firstName+lastName?


回答1:


Use CursorMatchers to match items in your listview.

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.

And the usage is the following:

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


来源:https://stackoverflow.com/questions/37030336/espresso-error-performing-load-adapter-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!