2017-04-10 65 views
0

我有一個列表視圖,包含具有相同ID的多行;每行有幾個不同的組件。這裏的層次結構(所有的知名度=可見)的摘錄:如何獲取某個列表元素的詳細信息?

->ListView{id=213, res-name=listview, child-count=2} 
-->RelativeLayout{id=214, res-name=bground, child-count=5} 
--->AppCompatTextView{id=215, res-name=name, text=Short} 
--->AppCompatTextView{id=216, res-name=valid, text=10.04.} 
--->... further elements in the RelativeLayout res-name=bground 
-->RelativeLayout{id=214, res-name=bground, child-count=5} 
--->AppCompatTextView{id=215, res-name=name, text=Group} 
--->AppCompatTextView{id=216, res-name=valid, text=09.04} 
--->... 

我想環路在此列表中所有的第一級別的元素(所有的RelativeLayout元素與RES-NAME = bground),並檢查name屬性;一旦我找到了我搜索的元素(例如name = Group),我想檢查它的其他屬性。

我的當前編碼是:

for (int i = 0; i < numItems; i++) { 
    String type = getDataText(allOf(withId(R.id.name), 
     childAtPosition(
      allOf(withId(R.id.bground), 
       childAtPosition(withId(R.id.listview),i)), 
      0), 
     isDisplayed())); 

其中getDataText是:

String getDataText(final Matcher<View> matcher) { 
    final String[] stringHolder = { null }; 
    onData(matcher).perform(new ViewAction() { 
     @Override 
     public Matcher<View> getConstraints() { 
      return isAssignableFrom(TextView.class); 
     } 

     @Override 
     public String getDescription() { 
      return "getting text from a TextView"; 
     } 

     @Override 
     public void perform(UiController uiController, View view) { 
      TextView tv = (TextView)view; 
      stringHolder[0] = tv.getText().toString(); 
     } 
    }); 
    return stringHolder[0]; 
} 

在該循環的執行如上所示,得到了一個AmbiguousViewMatcherException,其中列表視圖被標記爲問題。我想這意味着Espresso在listview元素中找到了不明確的子元素(什麼是真的),但是childAtPosition應該只取得位置索引指定的元素。

如何正確實現循環?

感謝和問候 格哈德

回答

0

您可以添加isDisplayed()到您的適配器視圖匹配與當前可見的列表視圖進行交互。

inAdapterView(allOf(withId(android.R.id.list),isDisplayed()))

+0

謝謝您的回答。不幸的是,我沒有得到它的工作。 – Gerhard

+0

同時我發現這個: 該應用程序顯示三個選項卡(第一個在屏幕上可見),所有三個選項卡在視圖層次結構中都具有visible = VISIBLE屬性。第一個也是第三個選項卡包含一個listview元素。 我預計,這應該解決: '... allOf(childAtPosition( allOf(childAtPosition( childAtPosition( withId(R.id.pager),1), 0),isDisplayed()), 0)/ * = listview * /,isDisplayed()),... 您能否再解釋一下您的解決方案? 謝謝,最好的問候 Gerhard – Gerhard

相關問題