
Question:
I am trying to get the count of elements with same rid
The solutions here <a href="https://stackoverflow.com/questions/27430500/how-to-get-count-of-items-with-same-ids-which-are-not-in-adapter-view" rel="nofollow">How to get count of items with same ids which are not in adapter view</a> is not helping me.
static int counter = 0;
public static Matcher<View> withIdAndDisplayed(final int id) {
Checks.checkNotNull(id);
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("with item id: " + id);
}
@Override
public boolean matchesSafely(View view) {
if ((view.getId() == id) && (view.getGlobalVisibleRect(new Rect())
&& withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE).matches(view))){
counter++;
return true;
}
return false;
}
};
}
Answer1:<strong>Update:</strong>
I see you trying to get child view count which is not an adapterView. See this one <a href="https://groups.google.com/forum/#!topic/android-test-kit-discuss/avLaBnBWr70" rel="nofollow">https://groups.google.com/forum/#!topic/android-test-kit-discuss/avLaBnBWr70</a>
<strong>Original answer:</strong>
Are you using RecyclerView?
I used below code in my tests to get the RecyclerView size.
public static Matcher<View> withRecyclerViewSize(final int size) {
return new TypeSafeMatcher<View>() {
@Override
public boolean matchesSafely(final View view) {
final int actualListSize = ((RecyclerView) view).getAdapter().getItemCount();
LOGD(TAG, "RecyclerView actual size " + actualListSize);
return actualListSize == size;
}
@Override
public void describeTo(final Description description) {
description.appendText("RecyclerView should have " + size + " items");
}
};
}
Usage: onView(withId(R.id.resource_id)).check(matches(withRecyclerViewSize(expectedSize)));
In this case resource_id
is RecyclerView.
There are few examples here: <a href="https://gist.github.com/chemouna/00b10369eb1d5b00401b" rel="nofollow">https://gist.github.com/chemouna/00b10369eb1d5b00401b</a>.