Android 基于ID筛选文本视图
我正在尝试验证一个应用程序的Android 基于ID筛选文本视图,android,android-listview,textview,android-relativelayout,robotium,Android,Android Listview,Textview,Android Relativelayout,Robotium,我正在尝试验证一个应用程序的TextView,该应用程序在搜索类别时显示所列产品的价格 我用的是自动化 获取当前视图时的层次结构(solo.getCurrentView(ListView.class))类似于: ListView --> (0)FrameLayout --> (1)RelativeLayout --> (0)ImageView --> (1)RelativeLayout -->(0)
TextView
,该应用程序在搜索类别时显示所列产品的价格
我用的是自动化
获取当前视图时的层次结构(solo.getCurrentView(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());
}
这种方法面临的问题是,我不知道如何根据ID
过滤这些视图。另外,在滚动时,这会从当前视图中跳过一个或两个产品,我无法修复
如果有人能就此提出建议,那就太好了。我将参考我之前的一个答案,您可以使用它在列表索引中查看
完成此操作后,您可以将该函数与一个新函数组合,以获取视图的所有子元素:
private List<View> getAllChildren(View v) {
if (!(v instanceof ViewGroup)) {
ArrayList<View> viewArrayList = new ArrayList<View>();
viewArrayList.add(v);
return viewArrayList;
}
ArrayList<View> result = new ArrayList<View>();
ViewGroup viewGroup = (ViewGroup) v;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
ArrayList<View> viewArrayList = new ArrayList<View>();
viewArrayList.add(v);
viewArrayList.addAll(getAllChildren(child));
result.addAll(viewArrayList);
}
return result;
}
私有列表getAllChildren(视图v){
如果(!(视图组的v实例)){
ArrayList viewArrayList=新建ArrayList();
viewArrayList.add(v);
返回viewArrayList;
}
ArrayList结果=新建ArrayList();
ViewGroup ViewGroup=(ViewGroup)v;
对于(int i=0;i
最后一个是按id过滤视图列表
public View(List<View> views, int idToMatch) {
for (View view : views) {
if (view.getId() == idToMatch) {
return view;
}
}
return null;
}
public视图(列表视图,int-idToMatch){
用于(视图:视图){
if(view.getId()==idToMatch){
返回视图;
}
}
返回null;
}
您可能会找到hwo来将这些功能组合到一个函数中,从而很好地实现您想要的功能。(提示R.id.price是一个整数,因此在您想要获取的索引中有子项列表后,只需将其传递到最后一个函数中即可)为什么不为每个ListView设置一个侦听器?这会有什么帮助?我想,一旦我点击列表项,我就会得到一个新的层次结构。抱歉,我是android automation的新手。请在实例化适配器并将其设置到ListView的位置添加代码
public View(List<View> views, int idToMatch) {
for (View view : views) {
if (view.getId() == idToMatch) {
return view;
}
}
return null;
}