【发布时间】:2014-02-21 09:52:47
【问题描述】:
我正在尝试验证应用程序的TextView,该应用程序在搜索类别时显示列出的产品的price。
我正在使用 Robotium 进行自动化。
当我获得当前视图 (solo.getCurrentViews(ListView.class)) 时的层次结构类似于:
ListView
--> (0)FrameLayout
--> (1)RelativeLayout
--> (0)ImageView
--> (1)RelativeLayout
-->(0)ImageView
-->(1)TextView //id - name
-->(2)TextView //id - price
-->(3)TextView //id - other
--> (2)RelativeLayout
--> (0)ImageView
--> (1)RelativeLayout
-->(0)ImageView
-->(1)TextView //id - name
-->(2)TextView //id - price
-->(3)TextView //id - other
--> (3)RelativeLayout
--> (0)ImageView
--> (1)RelativeLayout
-->(0)ImageView
-->(1)TextView //id - name
-->(2)TextView //id - price
-->(3)TextView //id - other
--> (4)RelativeLayout
--> (0)ImageView
--> (1)RelativeLayout
-->(0)ImageView
-->(1)TextView //id - name
-->(2)TextView //id - price
-->(3)TextView //id - other
--> (5)RelativeLayout
--> (0)ImageView
--> (1)RelativeLayout
-->(0)ImageView
-->(1)TextView //id - name
-->(2)TextView //id - price
-->(3)TextView //id - other
我可以想出两种获取价格的方法:
-
遍历列表视图的子视图并通过索引获取价格。就像下面的代码
int index = 1; ListView view = null; List<ListView> productList = solo.getCurrentViews(ListView.class); view = productList.get(0); for (int i = 1; i < view.getChildCount(); i++) { RelativeLayout relativeLayoutDetails = (RelativeLayout) view.getChildAt(index++); // get the first RelativeLayout RelativeLayout productDetails = (RelativeLayout) relativeLayoutDetails.getChildAt(2); //get the inner child RelativeLayout TextView productName = (TextView) productDetails.getChildAt(0); System.out.println("********" + productDetails.getChildCount()); TextView price = (TextView) productDetails.getChildAt(2); System.out.println(productName.getText().toString() + "---"+ price.getText().toString()); }
这段代码的问题是TextView price = (TextView) hotelDetails.getChildAt(2); 没有给我price,而是给了我other TextView(不知道为什么)。这也仅适用于当前视图。我无法向下滚动完整列表并获取每个产品的详细信息。
-
其次,获取当前视图的所有
TextViews 并根据priceid 过滤它们。如下所示:ListView l = (ListView) solo.getView(android.R.id.list); int viewShown = l.getChildCount();
int total = l.getCount(); int i; int index=1; if (hotelsShown != 0) { for (i = 0; i < total / viewShown; i++) { List<TextView> textViews = solo.getCurrentViews(TextView.class, l); //get all the text views //filter for price for(TextView priceView: textViews){ if(priceView.id equals "price-id") //get this TextView. } solo.scrollDown(); solo.sleep(500); }
我使用这种方法面临的问题是我不知道如何根据ID 过滤这些视图。此外,在滚动时,这会从当前视图中跳过一两个产品,并且无法解决此问题。
如果有人能就此提出建议,那就太好了。
【问题讨论】:
-
为什么不为每个 ListView 设置一个 OnItemClickListener ?
-
这有什么帮助?一旦我点击我猜的列表项,我就会得到一个新的层次结构。抱歉,我是 android 自动化的新手。
-
添加您实例化适配器的代码并将它们设置到您的列表视图
标签: android android-listview textview android-relativelayout robotium