Find if the Layout has particular text view or not

I am having Linear Layouts like this :

enter image description here

Now some Linear Layouts have sub category and some don’t have it. Similary some have discount and same don’t have it.

So when I do something like this :

List<WebElement> allFieldsInLayout = driver.findElements(By.id("com.flipkart.android:id/product_list_product_item_layout"));
List<WebElement> allTitlesOnCurrentScreen = driver.findElements(By.xpath("//*[@resource-id='com.flipkart.android:id/product_list_product_item_main_text']"));
List<WebElement> allsubTitlesOnCurrentScreen   = driver.findElements(By.xpath("//*[@resource-id='com.flipkart.android:id/product_list_product_item_sub_text']"));
List<WebElement> allOfferPricesOnCurrentScreen = driver.findElements(By.xpath("//*[@resource-id='com.flipkart.android:id/product_list_product_item_price']"));
List<WebElement> allListPriceOnCurrentScreen   = driver.findElements(By.xpath("//*[@resource-id='com.flipkart.android:id/product_list_product_item_mrp']"));

And then try to print text inside them :

for(int i=0;i<allTitlesOnCurrentScreen.size();i++){

        System.out.println("TITLE : "+allTitlesOnCurrentScreen.get(i).getAttribute("text")
                        + "SUB TITLE : "+allsubTitlesOnCurrentScreen.get(i).getAttribute("text")
                        + "OFFER PRICE : "+allOfferPricesOnCurrentScreen.get(i).getAttribute("text")
                        + "LIST PRICE : "+allListPriceOnCurrentScreen.get(i).getAttribute("text")
        );
    }

I got Array Out of Bound exception. So I thought if its possible to get the resource ids of all child fields from outer Layout of this list. I tried like this :

for(int i=0;i<allFieldsInLayout.size();i++){
            List<WebElement> allElementsinCurrentLayout = allFieldsInLayout.get(i).findElements(By.xpath("//android.widget.RelativeLayout[@index='2']"));
            for(int j=0;j<allElementsinCurrentLayout.size();j++) {
                System.out.println("Layout " + allElementsinCurrentLayout.get(j));
            }
}

But it gives exception that

Cannot use xpath locator strategy from an element. It can only be used from the root element)

I want NULL in my list if I dont have corresponding sub title or if no discount is there. How to do it ?

At end I want list something like this for subtitle = [NULL,Black,Blue,Black] and for listPrice like this [NULL,“2499”,NULL,“2200”]