4

I have a view hierarchy like below:

GridView{id=2131362110, res-name=item_list_grid, 
|
+----->RelativeLayout{id=2131362124, res-name=item_image_thumb_layout
|
+------------->ImageView{id=2131362125, res-name=item_image
|
+----->RelativeLayout{id=2131362124, res-name=item_image_thumb_layout
|
+------------->ImageView{id=2131362125, res-name=item_image
|
+------>RelativeLayout{id=2131362124, res-name=item_image_thumb_layout
|
+------------->ImageView{id=2131362125, res-name=item_image
|
GridView{id=2131362110, res-name=item_list_grid, ...etc

I want to perfom click on one of the ImageView with id=item_image.

I can not use something like atPosition(x) together with onView so instead I used onData. I tried all of these:

onData(allOf(anything(),withId(R.id.item_image))).atPosition(0).perform(click());

onData(anything()).atPosition(0).perform(click());

onData(allOf(atPosition(0),withId(R.id.item_image))).perform(click());

But all resulted in

android.support.test.espresso.AmbiguousViewMatcherException: 'is assignable from class: class android.widget.AdapterView' matches multiple views in the hierarchy.

Any suggestions for this? Thanks!

philomath
  • 2,209
  • 6
  • 33
  • 45

1 Answers1

20

Your error message tell you have multiple views in your activity that extends form AdapterView so you have another ListView or GridView in your layout.

You can either select the AdapterView on the data layer. So select this AdapterView with items of type ItemModel

onData(is(instanceOf(ItemModel.class))).atPosition(0)
    .onChildView(withId(R.id.item_image)).perform(click());

or you can choose a specific AdapterView by id

onData(anything()).inAdapterView(withId(R.id.my_grid_view)).atPosition(0).
            onChildView(withId(R.id.item_image)).perform(click());
David Boho
  • 2,646
  • 20
  • 17
  • 1
    Yes, that's true. I have 2 gridviews (I've edited my questions). The thing is both my gridviews have the same id. So I'm unable to use your second solution. My temporarily solution is: onData(instanceOf(Item.class)).atPosition(0).inAdapterView(allOf(withId(R.id.item_list_grid), isDisplayed())).perform(click()); I will test again your first solution to see if it works. – philomath Jan 21 '15 at 17:02
  • 1
    If your gridviews are having the same ids, you are doing it wrong. – JaydeepW Mar 11 '15 at 16:28
  • this: `onData(anything()).inAdapterView(withId(R.id.my_grid_view)).atPosition(0).perform(click());` was enough for me. tnx :) – MHSaffari Nov 11 '19 at 13:55