Robotium获取ListView的文本

时间:2012-05-10 06:47:33

标签: android robotium

1:我有一个列表视图如下:

sort
game
play
home

我们如何在robotium中逐一或全部获取上述列表的所有文本。

2:我们是否能够在SD卡(模拟器)中创建文件。

请建议一种方式。

4 个答案:

答案 0 :(得分:1)

对于第一个问题,我的解决方案是:

RelativeLayout rowItem;
            for (int i = 0; i < listView.getChildCount(); i++) {
                rowItem = (RelativeLayout) listView.getChildAt(i);
                TextView tv = (TextView) rowItem
                        .findViewById(R.id.my_tv);
                        String text = tv.getText();

            }

第二个问题: 当然,你可以

答案 1 :(得分:0)

您可以使用solo.getCurrentTextViews(查看父级),其中父级是列表。

答案 2 :(得分:0)

回答第二个问题试试这个

Context ctx = getInstrumentation().getContext();
AssetManager assetManager = ctx.getAssets();
File a = ctx.getFilesDir();
a.createNewFile();

答案 3 :(得分:0)

以下是我如何从ListView项目中获取唯一名称的解决方案:

//check if ListView contains elements by getting ListView size
int index = 0;
int elemCount = 0;
ListView listView = (ListView) solo.getCurrentListViews().get(index);
elemCount = listView.getAdapter() != null ? listView.getAdapter().getCount() : 0;

//Creating a Map of existing elements 
Map<String, Integer> namesMap = new HashMap<String, Integer>();

//Going through the list of elements and mapping it's names
for (int i = 0; i < elemCount ; i++) {

  String name = ((StructureElement) listView.getAdapter().getItem(i)).getName();
  namesMap.put(name , i);

}

Log.i("Names and indexes are : " + namesMap);

只需创建另外的方法,如果您想通过它的名称获取ListItem索引,则返回namesMap,反之亦然&#34;如果你想通过它的索引获取ListItem名称。

随意重复使用它)