How to check for text in a ListView string that uses an adapter that extends BaseAdapter

I have an Activity that contains a ListView that I am trying to check for the existence of a specific record using Espresso. ListView data is displayed using an adapter that extends BaseAdapter.

public class CommentListAdapter extends BaseAdapter {
    private static LayoutInflater inflater = null;
    Context context;
    List<Comment> comments;

    public CommentListAdapter(Context context, List<Comment> comments) {
        this.context = context;
        this.comments = comments;
        inflater = (LayoutInflater) context

    public int getCount() {
        return comments.size();

    public Object getItem(int position) {
        return comments.get(position);

    public long getItemId(int position) {
        return position;

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = inflater.inflate(R.layout.comment_row, null);

        TextView timestamp = (TextView) view.findViewById(;
        timestamp.setText(new SimpleDateFormat("dd-MM-yyyy hh:mm:ss").format(comments.get(position).getTimeStamp()));

        TextView commentText = (TextView) view.findViewById(;

        return view;


The field I need to interrogate is the field. Looking at the Espresso documentation I saw an example for the SimpleAdapter, but can't figure out how to refactor it to work with my CommentAdapter.

I really wonder if I should use SimpleAdapter or ArrayAdapter, however I went down the path of extending BaseAdapter because another article recommended it. This incorrectly changes my implementation, just to run the Espresso test because it is not clear how to write the test code.

Please can anyone help?


The solution I came across was to use ArrayAdapter instead of extending BaseAdapter and creating the following Matcher

public class CommentListAdapterMatchers {
    public static Matcher<Object> withContent(String expectedText) {
        return withContent(equalTo(expectedText));

    public static Matcher<Object> withContent(final Matcher<String> expectedObject) {
        return new BoundedMatcher<Object, Comment>(Comment.class) {
            public boolean matchesSafely(Comment actualObject) {
                return expectedObject.matches(actualObject.getComment());

            public void describeTo(Description description) {
                description.appendText("with content: " + description);


Finally, I used the following call onData p>

onData(allOf(is(Comment.class), CommentListAdapterMatchers.withContent("My Comment4"))).check(matches(isDisplayed()));


This worked on my test project, but then once I integrated it into a more complex layout I needed to tell the ListView I was interested in using inAdapterView

onData(allOf(is(Comment.class), CommentListAdapterMatchers.withContent("My Comment4"))).inAdapterView(withId(;



source to share

All Articles