【问题标题】:Filter TextViews based on ID根据 ID 过滤 TextView
【发布时间】: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

我可以想出两种获取价格的方法:

  1. 遍历列表视图的子视图并通过索引获取价格。就像下面的代码

    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(不知道为什么)。这也仅适用于当前视图。我无法向下滚动完整列表并获取每个产品的详细信息。

  1. 其次,获取当前视图的所有TextViews 并根据price id 过滤它们。如下所示:

    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


【解决方案1】:

我将参考我之前的一个答案,您可以使用它来查看列表索引here

一旦你有了这个,你就可以把这个函数和一个新的函数结合起来,得到一个视图的所有子元素:

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;
}

最后最后一个是按id过滤视图列表

public View(List<View> views, int idToMatch) {
    for (View view : views) {
        if (view.getId() == idToMatch) {
            return view;
        }
    }
    return null;
}

您可能会想出如何将所有这些组合成一个函数,该函数可以很好地完成您想要的操作。 (提示 R.id.price 是一个整数,因此只要在您想要获取的索引处拥有子列表,只需将其传递到最后一个函数即可)

【讨论】:

    猜你喜欢
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2019-06-08
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    相关资源
    最近更新 更多