Espresso. Error performing 'load adapter data&

2019-04-19 07:02发布

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条回答
Fickle 薄情
2楼-- · 2019-04-19 07:34

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());
查看更多
登录 后发表回答